5

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?

jameszhao00
  • 7,213
  • 15
  • 62
  • 112
  • Perhaps you haven't specified c++0x in your compiler settings. – JoshD Oct 03 '10 at 02:26
  • 4
    That's not the C++0x spec. The C++0x spec is still in draft and awaiting approval, and looks nothing like wikipedia. Here is the "Final Committee Draft": http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf – Ben Voigt Oct 03 '10 at 02:52

3 Answers3

8

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
  • 51,091
  • 10
  • 150
  • 143
  • 6
    @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 Oct 03 '10 at 02:52
  • Good point! Thank you for that clarification. Too used to Java/C# I guess :( – jameszhao00 Oct 03 '10 at 04:12
  • [Compiler support for upcoming C++0x](http://stackoverflow.com/questions/980573/compiler-support-for-upcoming-c0x/980621#980621) –  Oct 03 '10 at 04:51
0

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
  • 8,246
  • 2
  • 25
  • 26
0

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

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345