Operator Overloading and boilerplate code

Ali Çehreli acehreli at yahoo.com
Mon Jul 4 18:11:43 PDT 2011


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


More information about the Digitalmars-d-learn mailing list