Default Argument Improvements?

Brian Byrne bdbyrne at wisc.edu
Tue Jan 30 16:32:29 PST 2007


Hello,

One of the things that annoyed me in C++ was when I had a base class constructor with which I wanted to supply default argument(s) for, but then have a derived class with one or more extended parameters (no default value) on top of the base's parameters. It's impossible to append the derived class's new parameter(s) to the end of the base's while keeping the same order since the original default parameter is wrenched in there. Example:

class A {
    this( int x, int y = 0 ) {
        ...
    }
}

class B : A {
    this( int x, int y, int z ) { // Same parameter order
        super( x, y );
        ...
    }
    this( int x, int z ) { // Requires duplication of function
        super( x );        // to get default argument effect
        ...
    }
}



Of course this applies in the general case not limited to member constructors. Is there a reason that default arguments must be at the end of a function? Why not allow:

void foo( int a, int b = 0, int c ) { ... }

Which could be called by:

foo( 5, , 10 ); // empty gap for default
foo( 5, void, 10 ); // or more explicit
foo( 5, ..., 10 ); // or can the ellipsis be unambiguously used?

To extend it even further, what if I had foo overloaded:

void foo( int a, real b = 1.5, int c ) { ... }

Then the above calling methods would be problematic. Could inserting the type in the calling statement be used to disambiguate the functions?

foo( 5, int, 10 ); // matches foo(int, int, int); a = 5, b = 0, c = 10
foo( 0, real, 20 ); // matches foo(int, real, int); a = 0, b = 1.5, c = 20

and likewise going back to just having the first foo function:
foo( 5, auto, 10 ); // matches foo(int, int, int)





More information about the Digitalmars-d-learn mailing list