Sign in to follow this  
Quat

C++11 in class initialization

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

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

Edited by Alessio1989

Share this post


Link to post
Share on other sites

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.

Edited by NightCreature83

Share this post


Link to post
Share on other sites

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.

Edited by pinebanana

Share this post


Link to post
Share on other sites

 

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!).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this