Class template argument deduction from constructor call

"Jérôme M. Berger" jeberger at free.fr
Wed Oct 27 13:02:44 PDT 2010


div0 wrote:
> 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.
> 
	Why would it only be able to work when there is exactly one
constructor? Doesn't function overloading work with templates?

	This works here:

void foo(T) (T x) {
}

void foo(T, U) (T x, U y) {
}

void main()
{
    foo (3);
    foo (3, "azerty");
}

		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/20101027/6f3ed190/attachment.pgp>


More information about the Digitalmars-d-learn mailing list