Struct initializers and const in 2.009

torhu no at spam.invalid
Sun Jan 6 05:07:45 PST 2008


---
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.



More information about the Digitalmars-d mailing list