On Wednesday, 14 November 2012 at 06:52:57 UTC, Rob T wrote:
In C++ there are conversion operators, which are not exactly the same as function overloading, but the correct function is selected based on the type on the left hand side.

Example

class A
{
   operator bool(){ return _b; }
   operator int(){ return _i; }
   int _i; bool _b;
}

A a;
bool b = a; // executes bool()
int i = a; // executes int()

Is there anything like C++ conversion operators in D? I have used conversion ops in C++ and may want to use a similar feature in D if available.


--rt

Not really, that has *nothing* to do with "the correct function is selected based on the type on the left hand side", but just C++ trying to match the signature of the constructor it is trying to call. As a matter of fact, as a rule of thumb, there is no "left hand side" for the compiler. Just functions and arguments.

It is absolutely no different from:
//----
void foob(bool);
void fooi(int);
A a;
foob(a); //Executes bool()
fooi(a); //Executes int()
//----

Reply via email to