Re: Class template argument deduction from constructor call

2010-10-28 Thread Jérôme M. Berger
Steven Schveighoffer wrote:
 On Wed, 27 Oct 2010 16:26:21 -0400, div0 d...@sourceforge.net wrote:
 
 class Foo(T) {
 this(T t) {
 bar = t;
 }

 this(string x) {
 }

 this(int x) {
 }

 T bar;
 }

 autof0 = new Foo(wtf?);
 autof1 = 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:jeber...@free.fr
http://jeberger.free.fr
Jabber: jeber...@jabber.fr



signature.asc
Description: OpenPGP digital signature


Re: Class template argument deduction from constructor call

2010-10-27 Thread div0

On 27/10/2010 20:36, sergk wrote:

class Foo(T) {
 this(T t) {
 bar = t;
 }
 T bar;
}

void main() {
 auto a = new Foo(123); // doesn't work
 auto b = new Foo!(int)(123); // work, but redundant
}

Is there any technical limitations preventing this, or its just a compiler bug?


It's not a bug.

I guess it could be a short cut, but it can only ever work when the 
class has exactly one constructor, which seems a bit of a pointless 
short cut and adds an unneccassiry corner case to the language spec.


--
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk