Thread overview
Instantiating template classes with default template arguments
Feb 14, 2007
Sean Kelly
Feb 15, 2007
Bruno Medeiros
Feb 15, 2007
Sean Kelly
Feb 16, 2007
Bruno Medeiros
Feb 15, 2007
janderson
February 14, 2007
Currently, this is illegal:

    class C( T = int ) {}

    void main() {
        C val = new C;
    }

However, I'm not sure I've ever completely understood the reason for it. 
 The intent seems clear: use class type C with the default template arguments.  From a 1000' perspective, this seems no different than the following legal case (please note that the leading default is useless):

    class C( T = int, T = int ) {}

    void main() {
        C!(int) val = new C!(int);
    }

Resolving specializations isn't an issue either, since resolving with with one supplied argument is the same as resolving with no supplied arguments.  I don't suppose there is any chance of this changing?  And if not, can someone please explain why?

For what it's worth, I realize that the following works:

    class C( T = int ) {}

    void main() {
        C!() val = new C!();
    }

but the "!()" just amounts to semantic noise IMO.  Is it truly necessary, given that the symbol "C" must be unique anyway?  As a use case, it would allow this:

    class String( T = char ) {}
    alias String!(wchar) WString;

instead of this:

    class StringImpl!( T ) {}
    alias StringImpl!(char)  String;
    alias StringImpl!(wchar) WString;

which currently makes extending library classes somewhat confusing to new users (reference the std::basic_string issue in C++).


Sean

P.S. In writing this, I discovered that the following code compiles when it should not:

    class C( T = int, U = int ) {}
    class C( T = int, U : char = int ) {}

    void main()
    {
        auto c = new C!(int);
    }
February 15, 2007
Sean Kelly wrote:
> 
> P.S. In writing this, I discovered that the following code compiles when it should not:
> 
>     class C( T = int, U = int ) {}
>     class C( T = int, U : char = int ) {}
> 
>     void main()
>     {
>         auto c = new C!(int);
>     }

What's wrong with that?

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
February 15, 2007
Sean Kelly wrote:
> 
> Sean
> 
> P.S. In writing this, I discovered that the following code compiles when it should not:
> 
>     class C( T = int, U = int ) {}
>     class C( T = int, U : char = int ) {}
> 
>     void main()
>     {
>         auto c = new C!(int);
>     }

It looks like the compiler doesn't evaluate the template until its used. 
 I'm down with that.
February 15, 2007
Bruno Medeiros wrote:
> Sean Kelly wrote:
>>
>> P.S. In writing this, I discovered that the following code compiles when it should not:
>>
>>     class C( T = int, U = int ) {}
>>     class C( T = int, U : char = int ) {}
>>
>>     void main()
>>     {
>>         auto c = new C!(int);
>>     }
> 
> What's wrong with that?

Oops... you're right.  I edited that from my original example:

    class C( T = int, U = int ) {}
    class C( T = int, U : char = char ) {}

    void main()
    {
        auto c = new C!(int);
    }

This one shouldn't compile and does.  Unless the defaults are always chosen from the lexically first match?



Sean
February 16, 2007
Sean Kelly wrote:
> Bruno Medeiros wrote:
>> Sean Kelly wrote:
>>>
>>> P.S. In writing this, I discovered that the following code compiles when it should not:
>>>
>>>     class C( T = int, U = int ) {}
>>>     class C( T = int, U : char = int ) {}
>>>
>>>     void main()
>>>     {
>>>         auto c = new C!(int);
>>>     }
>>
>> What's wrong with that?
> 
> Oops... you're right.  I edited that from my original example:
> 
>     class C( T = int, U = int ) {}
>     class C( T = int, U : char = char ) {}
> 
>     void main()
>     {
>         auto c = new C!(int);
>     }
> 
> This one shouldn't compile and does.  Unless the defaults are always chosen from the lexically first match?
> 
> 
> 
> Sean

The template that matches is the char one, the second one. It matches that one regardless of lexical order. I suspect that's because it is a specialization, thus having more priority. For instance, the following fails with an ambiguity error:


    class C( T = int, U : int = int ) { pragma(msg,"int");}
    class C( T = int, U : char = char ) { pragma(msg,"char"); }


    void main()
    {
//template instance C!(int) matches more than one template declaration
        auto c = new C!(int);
    }




-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D