www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Instantiating template classes with default template arguments

reply Sean Kelly <sean f4.ca> writes:
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);
     }
Feb 14 2007
next sibling parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
Feb 15 2007
parent reply Sean Kelly <sean f4.ca> writes:
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
Feb 15 2007
parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
Feb 16 2007
prev sibling parent janderson <askme me.com> writes:
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.
Feb 15 2007