www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - opImplictCast questions

reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
When opImplicitCast comes along, if I do the following:

typedef int X;

struct Y
{
   X opImplicitCast() {...}
}

This should compile:

Y y;
X x = y;

Should this?

Y y;
int i = y;

(hint hint: I would like it NOT to compile)

Also, I know const is out and that's great and all, but when do we get 
opImplicitCast?

Not to be pushy or anything :)

-Steve 
Nov 29 2007
parent Regan Heath <regan netmail.co.nz> writes:
Steven Schveighoffer wrote:
 When opImplicitCast comes along, if I do the following:
 
 typedef int X;
 
 struct Y
 {
    X opImplicitCast() {...}
 }
 
 This should compile:
 
 Y y;
 X x = y;
 
 Should this?
 
 Y y;
 int i = y;
 
 (hint hint: I would like it NOT to compile)
 
 Also, I know const is out and that's great and all, but when do we get 
 opImplicitCast?
 
 Not to be pushy or anything :)

My gut feeling is that it will not compile, unless you change "typedef" to "alias" and then it will. I believe D's typedef creates such a unique/separate type that even though it is based on 'int' (in this case) it does not even 'inherit' the implicit conversions of 'int', let alone implicitly convert to it's base type. R
Nov 30 2007