30

I've got a situation which can be summarized in the following:

class Test
{

    Test();

    int MySet[10];

};

is it possible to initialize MySet in an initializer list?

Like this kind of initializer list:

Test::Test() : MySet({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {}

Is there any way to initialize a constant-sized member array in a class's initalizer list?

Serge
  • 1,974
  • 6
  • 21
  • 33
  • 3
    For what it's worth, `Set` isn't just a pointer to an array of `10` integers, nor is it `static` here. Rather, the array name `Set` decays to a pointer to the first element of the array in certain situations. The difference can be seen clearly using `sizeof` - i.e. `sizeof(Set) == 10 * sizeof(int) != sizeof(int*)`. – Stuart Golodetz Aug 06 '12 at 22:59
  • 2
    @StuartGolodetz Thanks for the clarification. When I said static, I meant static in the form that it is stored with the object/instance, and not just somewhere else in the heap. Of course, that's a gross misuse of the term `static` on my part; sorry. – Serge Aug 06 '12 at 23:00
  • No worries :) I was mainly just trying to clarify the distinction between arrays and pointers on the off-chance that there might have been a misunderstanding there. – Stuart Golodetz Aug 06 '12 at 23:53

3 Answers3

30

While not available in C++03, C++11 introduces extended initializer lists. You can indeed do it if using a compiler compliant with the C++11 standard.

struct Test {
    Test() : set { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } { };
    int set[10];
};

The above code compiles fine using g++ -std=c++0x -c test.cc.


As pointed out below me by a helpful user in the comments, this code does not compile using Microsoft's VC++ compiler, cl. Perhaps someone can tell me if the equivalent using std::array will?

#include <array>

struct Test {
  Test() : set { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } } { };
  std::array<int, 10> set;
};

This also compiles fine using g++ -std=c++0x -c test.cc.

obataku
  • 29,212
  • 3
  • 44
  • 57
  • 3
    Another good reason to love C++11 (or some parts of it)! =D Thank you so much! – Serge Aug 06 '12 at 23:03
  • 5
    Note that this code does not compile with Visual C++ version 11 and earlier (i.e., it does not compile with Microsoft Visual Studio 2012, and Visual C++ is the main compiler for the most common platform). So, **if you want portable code, don't do this**. Yet. – Cheers and hth. - Alf Aug 06 '12 at 23:18
  • Thank you @Cheersandhth.-Alf for I'm not a C++ programmer and wouldn't have known that. :) – obataku Aug 06 '12 at 23:27
  • 1
    @Serge you should make sure you heed the above advice in case you want portable code. Look into using `std::array` as an alternative which hopefully should work in Visual C++ 11 as well. – obataku Aug 06 '12 at 23:28
5

Unfortunately, in C++03, you cannot initialize arrays in initializer lists. You can in C++11 though if your compiler is newer :)

see: How do I initialize a member array with an initializer_list?

Community
  • 1
  • 1
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
3

"I understand that Set is just a pointer to the static array of 10 integers"

No, that's wrong: it's an array, not a pointer.

You can still initialize it in the constructor's initializer list.

For a compiler that doesn't support C++11 curly braces initialization (Visual C++ version 11 and earlier comes to mind) you'll have to jump through some hoops though, as shown below:

#include <iostream>
#include <vector>
using namespace std;

#define CPP11
#if defined( _MSC_VER )
#   if (_MSC_VER <= 1700)
#       undef CPP11
#   endif
#endif

#ifdef CPP11
class Cpp11
{
private:
    int set_[10];

public:
    Cpp11()
        : set_{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    {}

    int foo() const { return set_[3]; }
};
#endif

class Cpp03
{
private:
    struct IntArray10 { int values[10]; };
    IntArray10 set_;

    static IntArray10 const& oneToTen()
    {
        static IntArray10 const values =
            { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} };
        return values;
    }

public:
    Cpp03()
        : set_( oneToTen() )
    {}

    int foo() const { return set_.values[3]; }
};

int main()
{}

Instead of using raw arrays, though, use std::vector and C+++11 std::array, both of which are supported even by Visual C++ 11.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331