TDPL: Operator Overloading

Ali Çehreli acehreli at yahoo.com
Tue Aug 24 15:58:56 PDT 2010


Andrej Mitrovic wrote:
> Yao G. Wrote:
> 
>> That's a bug. The return value should be CheckedInt(result);
> 
> I'll add that to the errata.
> 
> 
> Yao G. Wrote:
> 
>> http://www.digitalmars.com/d/2.0/mixin.html
> 
> I wasn't refering to the mixin, but the call to CheckedInt(). mixin compiles "value" ~ op ~ "rhs.value", which in this case evaluates to 5 + 5 and the whole call becomes CheckedInt(10). 
> 
> What I don't understand is how you can construct a new CheckedInt struct by calling it with CheckedInt(10), when I have to use a call like CheckedInt!(int)(10) outside the struct (in main or in a unittest block).

It is the same in C++: the name of the template is equivalent to the 
current instantiation of the template.

foo() and bar() are both legal:

template <class T> class C
{
public:

     C foo()
     {
         return C();
     }

     C<T> bar()
     {
         return C<T>();
     }
};

int main()
{
     C<int> c;
     c.foo();
     c.bar();
}

It seems to be the same in D. I don't know whether this is intended, or 
just a left over from the C++ parts of dmd. (I assume dmd shares code 
with the Digital Mars C++ compiler.)

Ali


More information about the Digitalmars-d-learn mailing list