Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++11 in class initialization


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
6 replies to this topic

#1   Members   -  Reputation: 545

Like
0Likes
Like

Posted 30 October 2013 - 04:30 PM

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

#2   Crossbones+   -  Reputation: 33263

Like
0Likes
Like

Posted 30 October 2013 - 04:58 PM

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, 30 October 2013 - 04:59 PM.

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | [Fly with me on Twitter]

#3   Members   -  Reputation: 1370

Like
0Likes
Like

Posted 30 October 2013 - 05:04 PM

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, 30 October 2013 - 05:07 PM.


#4   Members   -  Reputation: 4273

Like
0Likes
Like

Posted 30 October 2013 - 05:09 PM

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, 30 October 2013 - 05:10 PM.

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

#5   Crossbones+   -  Reputation: 4696

Like
2Likes
Like

Posted 31 October 2013 - 01:22 AM

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, 31 October 2013 - 01:23 AM.

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


#6   Members   -  Reputation: 475

Like
0Likes
Like

Posted 03 November 2013 - 07:55 AM

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, 03 November 2013 - 07:55 AM.

anax - An open source C++ entity system


#7   Crossbones+   -  Reputation: 33263

Like
0Likes
Like

Posted 03 November 2013 - 06:41 PM

 

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


It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | [Fly with me on Twitter]




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.




PARTNERS