Monday, May 5, 2008

Tell me why!


Most C++ quirks can be argued by efficency or inheritence reasons. But sometimes I just don't see why there is yet another exception in the rules of the language. Such an example is the initialization of static constant data members:

Section 9.4.4 of the ISO standard says:
If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression.
Here is an example of this:

class X {
    static const int i = 23;
};

Now, I wonder why this in-place initialization is restricted to those two types. I would like to be a able to express:


class A;
class X {
public:
    static const A* special_meaning=0;
    void do_something(const A* with=special_meaning);
};

C++ prevents me from writing descriptive code in this case. And for what reason? I can't see any.

By the way, section 9.4.4 adds:

The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.
Sure, linker and such... So what is this in-place initialization good for after all?

 
Thank you, Liesa

No comments:

Post a Comment