Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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 Quat   Members   -  Reputation: 424

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

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 22732

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' 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] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#3 N.I.B.   Members   -  Reputation: 1250

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 Alessio1989   Members   -  Reputation: 2403

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.


#5 NightCreature83   Crossbones+   -  Reputation: 3287

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, Mad Max

#6 pinebanana   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 Servant of the Lord   Crossbones+   -  Reputation: 22732

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' 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] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





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