On Thursday, 23 May 2013 at 10:17:00 UTC, Peter Alexander wrote:
On Thursday, 23 May 2013 at 09:05:02 UTC, Don wrote:
This means that the const variable x has been initialized
TWICE!
That's no different from non-const members.
It's perfectly OK to modify a non-const member as many times as
you like. That doesn't cause confusion.
struct Foo { int x = 1; }
Foo f = Foo(2); // f.x is 2
The initialiser is a default value if you don't provide one in
the constructor. If you don't mark a variable as static then it
is not static and needs to be initialised like any other member
variable.
What gives you that idea? It's listed as an initializer in the
spec.
It's implemented as an initializer in the compiler.
This new behaviour is counter-intuitive and introduces a
horrible inconsistency.
It is exactly what happens in C++ and causes no confusion there.
I don't think it's legal in C++:
struct S
{
const int x = 5;
};
w.cpp:4:17: error: ISO C++ forbids initialization of member ‘x’
[-fpermissive]
This is totally different to what happens with module
constructors (you get a compile error if you try to set a
const global if it already has an initializer).
In structs/classes, it is not an initialiser, it is a default
value in case you don't provide a different value.
As far as I can tell, this new feature exists only to create
bugs. No use cases for it have been given. I cannot imagine a
case where using this feature would not be a bug.
The use case is simple: to allow non-static const member
variables.
Not correct. You've always been able to have non-static const
member variables, as long as they have no initializer.
What this feature does, is allow you to add an initializer which
is ignored.