Class template argument deduction from constructor call

"Jérôme M. Berger" jeberger at free.fr
Thu Oct 28 11:11:55 PDT 2010


Steven Schveighoffer wrote:
> On Wed, 27 Oct 2010 16:26:21 -0400, div0 <div0 at sourceforge.net> wrote:
> 
>> class Foo(T) {
>>         this(T t) {
>>             bar = t;
>>         }
>>
>>     this(string x) {
>>     }
>>
>>     this(int x) {
>>     }
>>
>>         T bar;
>> }
>>
>> auto    f0 = new Foo("wtf?");
>> auto    f1 = new Foo(42);
>>
>> What's T in any of the above?
> 
> translates to:
> auto f0 = new Foo!(string)("wtf?");
> auto f0 = new Foo!(int)(42);
> 
> Both of which error, since T can be neither int nor string, or Foo would
> contain conflicting constructors.
> 
> Your question is akin to asking why IFTI doesn't work on something like
> this:
> 
> T foo(T)(int x);
> 
> What Jerome was referring to is something like this:
> 
> class Foo(T)
> {
>   this(T t) {}
>   this(T t, string x);
> }
> 
> which should be unambiguous and completely doable.
> 
> The thing is, when a templated class is to be instantiated without
> giving a complete set of template arguments, then it should use IFTI. 
> The decision to try IFTI is not ambiguous, but depending on how you
> implement the constructors, the overloading can be ambiguous.
> 
	Exactly, it should be possible to have consistent behaviour for
template class constructors and template functions: they should both
work and fail in similar circumstances.

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20101028/529635b0/attachment.pgp>


More information about the Digitalmars-d-learn mailing list