Jump to content
  • Advertisement
Sign in to follow this  
Quat

C++11 in class initialization

This topic is 1721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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.

Edited by satanir

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!