www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Constructor call must be in a constructor

reply Loopback <elliott.darfink gmail.com> writes:
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?
Jul 06 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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
Jul 06 2011
parent Don <nospam nospam.com> writes:
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.
Jul 07 2011