Operator Overloading and boilerplate code

Loopback elliott.darfink at gmail.com
Tue Jul 5 07:20:44 PDT 2011


On 2011-07-05 03:11, Ali Çehreli wrote:
> On Tue, 05 Jul 2011 02:44:03 +0200, Loopback wrote:
>
>> I've researched a bit though I still haven't come up with a solution.
>> Since the problem lies within (the most simple) constructor, I tried to
>> modify it for another outcome. If I supplied a generic parameter to the
>> pre-constructor the "Cannot evaluate at compile time" message
>> disappeared but two new errors appeared instead.
>>
>> This is what I modified:
>>
>> this()(float x, float y, float z) =>  this(T)(float x, float y, float z)
>>
>> If I use this code instead, I get two other errors appearing:
>>
>> Error: template test.DVECTOR2.__ctor(T) does not match any function
>> template declaration
>>
>> This error and another one (individual to each statement) appears in the
>> following code statements:
>>
>>
>> Error: template test.DVECTOR2.__ctor(T) cannot deduce template function
>> from argument types !()(float,float)
>> DVECTOR2 m_zoom = DVECTOR2(2f, 2f);
>>
>> Error: template test.DVECTOR2.__ctor(T) cannot deduce template function
>> from argument types !()(immutable(float),const(float)) immutable
>> DVECTOR2 m_UP_DIR = DVECTOR2(0f, 1f, 0f);
>
> Here is a simple form of the same problem:
>
> struct S
> {
>      this(T)(double d)
>      {}
> }
>
> void main()
> {
>      auto o = S(1.5);
> }
>
> Error: template deneme.S.__ctor(T) does not match any function template
> declaration
> Error: template deneme.S.__ctor(T) cannot deduce template function from
> argument types !()(double)
>
> The compiler is right: What should T be there? int? string? MyClass?
>
> I've realized again that I don't know how to specify the template
> parameter for the constructor. The following attempt fails as the
> compiler thinks S itself is a template:
>
>      auto o = S!string(1.5);
>
> Error: template instance S is not a template declaration, it is a struct
>
> And if I try to be smart after the error message, this seg faults the
> compiler:
>
>      auto o = S.__ctor!string(1.5);
>
> Ali
Hmm... Interesting. Thank you for clarifying and explaining that!

I guess supplying T to the constructor when the parameters are already
known to avoid compiler errors is not a solution then. Seems to me as if
its only aggravates things.

Though is there no solution nor any workarounds for this problem? I've
attempted to use two different types of constructors and both appeared
to be very limited, and I do not believe that this is the case.

If you use a generic constructor is there no possible way to use it in
cases where immutable and const is involved? Or is there a page
that I have missed perhaps?
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: vector.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110705/6ae32c2b/attachment.ksh>


More information about the Digitalmars-d-learn mailing list