Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x Peer Constructor in VC2010

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?

like image 485
jameszhao00 Avatar asked Oct 03 '10 02:10

jameszhao00


1 Answers

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; 
};
like image 192
In silico Avatar answered Oct 18 '22 04:10

In silico