Struct initializers and const in 2.009
Sean Kelly
sean at f4.ca
Sun Jan 6 09:41:35 PST 2008
torhu wrote:
> ---
> struct Foo
> {
> int val;
> }
>
> struct Bar {
> Foo f;
> //const Foo f; // workaround
> }
>
> const Foo foo = { 123 }; // line 11
>
> const Bar bar = {
> f: foo // the problem is here
> };
> ---
> bug.d(11): Error: cannot implicitly convert expression (Foo(123)) of
> type const(Foo) to Foo
>
> The problem here is that Bar.f is not const, while foo is. Since bar is
> const, shouldn't consts always be legal in the initializer? Or doesn't
> it work that way?
>
> The workaround for this is pretty simple, I know. foo could also be
> made an enum, but I need 1.x compatibility.
Weird. With transitive const, I'd expect the assignment to be from
const(Foo) to const(Foo).
Sean
More information about the Digitalmars-d
mailing list