Thread overview
Constructor call must be in a constructor
Jul 06, 2011
Loopback
Jul 06, 2011
Jesse Phillips
Jul 07, 2011
Don
July 06, 2011
Hi!

While implementing and overloading several different operators for my structure I've got stuck with an error.

As noticed in the attachment, in my opBinaryRight function I mimic the opBinary (left) operator by instantiating the structure itself to avoid implementing duplicates of the binary operator overloads.

The opBinaryRight operator is defined as following:

DVector2 opBinaryRight(string op, T)(T lhs) if(Accepts!T)
{
	// Error: template instance vector.DVector2.__ctor!(DVector2) error
instantiating
	return DVector2(lhs).opBinary!op(this);
}

I create an additional DVector2 structure and then calls the opBinary operator. When creating this DVector2 structure the following constructor gets called:

this(T)(T arg) if(Accepts!T)
{
	static if(isScalar!T)
		this(arg, arg);
	else
		// Error: constructor call must be in a constructor
		this(arg.tupleof);
}

As one can clearly see, the constructor call is within a constructor. Now my questions are; is this a bug with DMD or is it something with my code example and is there any workarounds/solutions?


July 06, 2011
Loopback Wrote:

> Hi!
> 
> While implementing and overloading several different operators for my structure I've got stuck with an error.
> 
> As noticed in the attachment, in my opBinaryRight function I mimic the opBinary (left) operator by instantiating the structure itself to avoid implementing duplicates of the binary operator overloads.
> 
> The opBinaryRight operator is defined as following:
> 
> DVector2 opBinaryRight(string op, T)(T lhs) if(Accepts!T)
> {
> 	// Error: template instance vector.DVector2.__ctor!(DVector2) error
> instantiating
> 	return DVector2(lhs).opBinary!op(this);
> }
> 
> I create an additional DVector2 structure and then calls the opBinary operator. When creating this DVector2 structure the following constructor gets called:
> 
> this(T)(T arg) if(Accepts!T)
> {
> 	static if(isScalar!T)
> 		this(arg, arg);
> 	else
> 		// Error: constructor call must be in a constructor
> 		this(arg.tupleof);
> }
> 
> [Blah, blah, blah]

This is probably related to a question recently asked on SO, which you might have even been the author. But for synergy: http://stackoverflow.com/questions/6553950/how-to-use-template-constructors-in-d
July 07, 2011
Jesse Phillips wrote:
> Loopback Wrote:
> 
>> Hi!
>>
>> While implementing and overloading several different operators for my
>> structure I've got stuck with an error.
>>
>> As noticed in the attachment, in my opBinaryRight function I mimic the
>> opBinary (left) operator by instantiating the structure itself to avoid
>> implementing duplicates of the binary operator overloads.
>>
>> The opBinaryRight operator is defined as following:
>>
>> DVector2 opBinaryRight(string op, T)(T lhs) if(Accepts!T)
>> {
>> 	// Error: template instance vector.DVector2.__ctor!(DVector2) error instantiating
>> 	return DVector2(lhs).opBinary!op(this);
>> }
>>
>> I create an additional DVector2 structure and then calls the opBinary
>> operator. When creating this DVector2 structure the following
>> constructor gets called:
>>
>> this(T)(T arg) if(Accepts!T)
>> {
>> 	static if(isScalar!T)
>> 		this(arg, arg);
>> 	else
>> 		// Error: constructor call must be in a constructor
>> 		this(arg.tupleof);
>> }
>>
>> [Blah, blah, blah]
> 
> This is probably related to a question recently asked on SO, which you might have even been the author. But for synergy: http://stackoverflow.com/questions/6553950/how-to-use-template-constructors-in-d

Bizarrely, the bit about template constructors was added to the docs as part of the fix to bug 2616.
Yet, bug 435 "Constructors should be templatized" is still open.
In view of this confusion, I would expect that compiler bugs related to this feature are very likely. Please submit a bug report.