The bizarre world of typeof()

Ary Borenszweig ary at esperanto.org.ar
Mon Oct 26 03:39:26 PDT 2009


Don wrote:
> I'm trying to make sense of the rules for 'typeof'. It's difficult 
> because DMD's behaviour is so different to the spec. Here's four simple 
> cases.
> 
> // This doesn't compile on D1.
> //alias typeof(int*int) Alias1;

Not valid: typeof accepts an expression and "int*int" is not a valid 
expression.

> 
> // This compiles in D1, but not in D2.
> alias int Int;
> alias typeof(Int*Int) Alias2;

Almost same as above: Int resolves to a type and "type*type" is not a 
valid expression.

> 
> // Yet this DOES compile on D2 !
> typeof(T*U) foo(T, U)(T x, U y) { return x*y; }
> alias typeof(foo(Int, Int)) Alias3;

Of course, because this doesn't translate to "Int*Int", this translates 
to some variables x and y of type "Int" and "Int" respectively for which 
you can do "x*y".



More information about the Digitalmars-d mailing list