views:

145

answers:

3

According to the C++0x spec, the following is legal

class A {
    A(int i) : x(i) {}
    A() : A(0) {}
    int x;
};

But it fails to compile ("A" is not a nonstatic data member or base class of class "A") in VC 2010. Anyone know what's wrong?

+7  A: 

Visual C++ 2010 (also known as VC++ 10.0) as of this writing does not support delegating constructors, which is what your code snippet requires. VC++ 10.0 only has partial support for C++0x, and as of this writing, no compiler has implemented the entire C++0x feature set (although that will change soon, especially once the C++0x standard is finalized).

Scott Meyers have a summary of C++0x support in gcc and MSVC compilers. Here's another list of C++0x feature support in different compilers. Also, a list of C++0x features supported in Visual C++ 2010 straight from the horse's mouth.

For now, initialize all members directly in the initialization list of your constructors:

class A
{ 
public:
    A(int i) : x(i) {} 
    A() : x(0) {} 
private:
    int x; 
};
In silico
Surprisingly, A() { A(0); } works for some reason.
jameszhao00
@jameszhao00: It "works" because that's not a delegating constructor. You're creating an unnamed temporary `A` which will be destructed once the constructor finishes, which is not what you want.
In silico
Good point! Thank you for that clarification. Too used to Java/C# I guess :(
jameszhao00
[Compiler support for upcoming C++0x](http://stackoverflow.com/questions/980573/compiler-support-for-upcoming-c0x/980621#980621)
Roger Pate
A: 

Visual Studio doesn't support all of 0x yet. (And nobody should be expected to; 0x isn't finalized.)

This describes what 0x features are implemented in VS 2010.

Steve M
+2  A: 

MSVC++ 2010 doesn't have support for delegating constructor

This page lists C+ 0x features and their support in popular compilers.

Prasoon Saurav