Thread overview | |||||
---|---|---|---|---|---|
|
July 06, 2011 Constructor call must be in a constructor | ||||
---|---|---|---|---|
| ||||
Attachments: | 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 Re: Constructor call must be in a constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Loopback | 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 Re: Constructor call must be in a constructor | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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.
|
Copyright © 1999-2021 by the D Language Foundation