PhobosWatch: manifest => enum

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Jan 4 11:55:07 PST 2008


Janice Caron wrote:
> On 1/4/08, Oskar Linde <oskar.lindeREM at ovegmail.com> wrote:
>> struct T {
>>         const int a = 5;
>>         int b;
>> }
>>
>> static assert(T.sizeof == int.sizeof);
> 
> I see a problem there. You said that a is template-like, and
> instantiated only if its address is taken. That means that T itself is
> template-like, because it contains a. But I don't see how it can be.

a is a constant known at compile time. It doesn't need to be part of the 
run time struct representation. There are two types of constants. Those 
known at compile time and those whose values are computed at run time. 
D1 uses two different keywords for those (const / final) and D2.009 
defines both using the same keyword (const). In D2.009:

struct T {
	const int a = 5;
	const int b;
	int c;
}

T t;

T.sizeof == 8 // a occupies no space

&t.a -> error 5 is not a lvalue
&t.b -> ok
&t.c -> ok

I'm not sure this behavior in D2.009 is intentional or not though.

&t.a could become legal (the template-like idea) but all instances of T 
would share the same a. So:

T x,y;

&x.a == &y.a;
&x.b != &y.b;

-- 
Oskar



More information about the Digitalmars-d mailing list