Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why are in class initializers not allowed?

142 views
Skip to first unread message

nvangogh

unread,
May 13, 2013, 4:55:01 PM5/13/13
to

In the C++ Standard - before C++11 - any attempt to initialize a
variable inside the body of a class would fail at compilation. I am
sure that there is / was a very good reason for this, but can't
understand why it is so.

I thought that maybe it was a restriction that was imposed due to some
form of resource limitation. I am sure that I am wrong.

Now C++11 allows in class initializers. This appears to make the class
design clearer - or at least much easier to implement for a novice
like me as there is no need for a lengthy & possibly erroneous
constructor initializer list.

So out of curiosity - why were (are) in class initializers prohibited
in old c++?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Wil Evers

unread,
May 14, 2013, 1:33:11 PM5/14/13
to
nvangogh wrote:

> In the C++ Standard - before C++11 - any attempt to initialize a
> variable inside the body of a class would fail at compilation. I am
> sure that there is / was a very good reason for this, but can't
> understand why it is so.
>
> I thought that maybe it was a restriction that was imposed due to some
> form of resource limitation. I am sure that I am wrong.
>
> Now C++11 allows in class initializers. This appears to make the class
> design clearer - or at least much easier to implement for a novice
> like me as there is no need for a lengthy & possibly erroneous
> constructor initializer list.
>
> So out of curiosity - why were (are) in class initializers prohibited
> in old c++?

You may be reading too much into this; I don't think there's any
fundamental reason why in-class initializers were disallowed in
C++98/03, although there are some weird corner cases. For an in-depth
discussion, see

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2628.html

Regards,

- Wil

Bart van Ingen Schenau

unread,
May 14, 2013, 5:07:51 PM5/14/13
to
On Mon, 13 May 2013 14:55:01 -0600, nvangogh wrote:

> So out of curiosity - why were (are) in class initializers prohibited in
> old c++?

Prior to being standardized, C++ did not allow *any* in-class
initializers, not even for static const integer members.
As far as I can determine, the philosophy was that initialization has no
place in a declaration, the initialization of non-static members is the
task of the constructor and the initialization of static members should
occur in the definition of that member.

To my knowledge, the languages that Bjarne Stroustrup got his ideas for C+
+ from did not have in-class initialization syntax either, so there was
no precedent to add it.

After C++ was standardized, new languages started to emerge that *did*
provide a syntax to provide in-class initializers and this probably led
to them being added to C++11.

Bart v Ingen Schenau
0 new messages