C++11 in class initialization

Started by
5 comments, last by Servant of the Lord 10 years, 5 months ago

I'm using visual studio 2013. How come this does not work:

class C
{
public:
std::vector<int> Data{ 1, 2, 3, 4 };
};

but this does:

class C
{
public:
std::vector<int> Data = std::vector<int>{ 1, 2, 3, 4 };
};

Also, how can I do in-class initialization for arrays:

class C
{
public:
int Data[4] = { 1, 2, 3, 4 }; // <--gives error
};

It seems kind of weak that they added initializer list support, but they don't work in-class.

-----Quat
Advertisement

Seems to work in GCC (or Clang, or whatever Ideone.com uses): http://ideone.com/WI9JmW

Does Visual Studio 2013 require you to enable C++11 features through some way? Earlier GCC versions required you to pass a command-line switch to enable them.

I believe C++11 is enabled by default. VS2013 doesn't fully support C++11, see details here.

[EDIT] also found this post, not exactly what you are seeing, but still vector initializer doesn't work correctly.

I'm using visual studio 2013. How come this does not work:

class C
{
public:
std::vector<int> Data{ 1, 2, 3, 4 };
};

but this does:

class C
{
public:
std::vector<int> Data = std::vector<int>{ 1, 2, 3, 4 };
};

Also, how can I do in-class initialization for arrays:

class C
{
public:
int Data[4] = { 1, 2, 3, 4 }; // <--gives error
};

It seems kind of weak that they added initializer list support, but they don't work in-class.

It seems it's a known bug... hopefully it will be fixed in a service pack... see the 13th comment: http://blogs.msdn.com/b/vcblog/archive/2013/10/17/visual-studio-2013-available-now.aspx?PageIndex=1#comments

"Recursion is the first step towards madness." - "Skegg?ld, Skálm?ld, Skildir ro Klofnir!"
Direct3D 12 quick reference: https://github.com/alessiot89/D3D12QuickRef/

I'm using visual studio 2013. How come this does not work:

class C
{
public:
std::vector<int> Data{ 1, 2, 3, 4 };
};

but this does:

class C
{
public:
std::vector<int> Data = std::vector<int>{ 1, 2, 3, 4 };
};

Also, how can I do in-class initialization for arrays:

class C
{
public:
int Data[4] = { 1, 2, 3, 4 }; // <--gives error
};

It seems kind of weak that they added initializer list support, but they don't work in-class.

You shouldnt pass a size for the array initialisation let the compiler figure out how many elements there are in the array. It looks like this:

int array[] =
{
    1,
    2,
    3,
    4,
    10000,
};

Array has size 5 and will be initialised with the values given, this only works for static arrays.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Seems to work in GCC (or Clang, or whatever Ideone.com uses): http://ideone.com/WI9JmW


MnYEXOh.png

It uses GCC 4.8.1. Just thought I'd mention it.

this only works for static arrays.

And only for the first element. e.g. int array[][] = { 1, 2, 3, 4 5, 6, 7, 8 } does not work, however, int array[][2] = { 1, 2, 3, 4, 5, 6, 7, 8 } } does.

anax - An open source C++ entity system

Seems to work in GCC (or Clang, or whatever Ideone.com uses): http://ideone.com/WI9JmW

It uses GCC 4.8.1. Just thought I'd mention it.

Thanks! I wasn't sure. smile.png

Ideone used to use GCC, but seemed like it started giving more Clang-y error messages recently (apparently GCC updated their output in the 4.8 series - nice!).

This topic is closed to new replies.

Advertisement