0

Since std::array hasn't an explicit constructor and my compiler doesn't support initialization list. I'd like to make a std::array with a function like

template<class T, int N>
std::array<T, N> makeArray(T const& t0 = 0, T const& t1 = 0, T const& t2 = 0,
                           T const& t3 = 0, T const& t4 = 0, T const& t5 = 0,
                           T const& t6 = 0, T const& t7 = 0, T const& t8 = 0)
{
 ...
}

Just list 9 elements, can be more. How to implement the function?

user1899020
  • 13,167
  • 21
  • 79
  • 154
  • `std::array myarray = {{t0, t1, t2, t3, t4, t5, t6, t7, t8}};` – Mooing Duck Mar 05 '14 at 18:00
  • 1
    What compiler are you using? The "initializion list" for `std::array` should work in any C++ compiler since 2003ish – Mooing Duck Mar 05 '14 at 18:00
  • -1 because the question's premise is fundamentally flawed. Sorry. – Lightness Races in Orbit Mar 05 '14 at 18:06
  • @LightnessRacesinOrbit What does that even mean? I can't figure out what the question's premise is. – Timothy Shields Mar 05 '14 at 18:07
  • 2
    @TimothyShields: The question's premise is that, because the OP's toolchain does not support C++11 initializer lists, the OP cannot initialise an `std::array` (i.e. with the symbols `{` and `}`). This premise is flawed because those `{` and `}` when used to initialise an `std::array` have absolutely nothing to do with C++11 initializer lists; they represent basic C++98 aggregate initialisation. The OP should have simply tried it out. – Lightness Races in Orbit Mar 05 '14 at 18:10
  • @LightnessRacesinOrbit Another point of view. The `{ ... }` syntax is the C++11 initializer list syntax. And his compiler may have been said to not support "C++11 initializer lists". So a conclusion may be "my code will not work". – Johannes Schaub - litb Mar 05 '14 at 18:18
  • @JohannesSchaub-litb: The `{ ... }` syntax is also the C++98 aggregate initialisation syntax. The OP's hypothetical code _will_ work (see David's answer). The OP should have simply tried it out. – Lightness Races in Orbit Mar 05 '14 at 18:19
  • @LightnessRacesinOrbit perhaps his examples used `array a{...}`. Trying out will give an error message on non-C++11 compilers. – Johannes Schaub - litb Mar 05 '14 at 18:22
  • @JohannesSchaub-litb: Then he should have tried it with a `=` ;) – Lightness Races in Orbit Mar 05 '14 at 18:23

1 Answers1

4

Implementing a make_array is a bad idea in general, but the way to create an std::array out of a set of values is using aggregate-initialization:

std::array<int,5> a = { 1, 2, 3, 4, 5 };

Note that it is not an std::initializer_list<int>.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • 4
    "Implementing a `make_array` is a bad idea in general," I and the author of [N3824](http://isocpp.org/files/papers/N3824.html) respectfully disagree. If for no other reason, than simply for the fact that `make_array` can deduce the size of the array from the number of initializers. – Casey Mar 05 '14 at 18:27
  • `make_array` does have its use cases, see for example [How to emulate C array initialization behaviour with std::array?](http://stackoverflow.com/q/6114067/341970) – Ali Mar 05 '14 at 19:41
  • also note that the upcoming C++14 `integer_sequence` machinery will let you define a `constexpr` (and also a `const`) `make_array`, which is very useful to generate large tables at compile-time inside your source code, rather than through a two-step build process. See [this Q&A](http://stackoverflow.com/a/19023500/819272), although that approach uses a generator function object than an initializer_list. – TemplateRex Mar 05 '14 at 20:13
  • Oh, yes... I imagine it is a great idea... try to initialize an array with the contents of 10 books using the interface in the question and see how it goes. Of course you can improve the interface and move out of the arguments (taken by value), but you might eventually run into exactly the same issues if the types are not movable. A facility like `make_array` hides potentially expensive operations and lowers some of the guarantees in the standard (order of evaluation of the initializers becomes undefined as soon as they become function arguments) – David Rodríguez - dribeas Mar 05 '14 at 22:40
  • I know others like the idea, but I reserve the right to disagree with even well known authors (for example with *almost always auto* I disagree with Sutter and Stroustrup, but only time will tell) – David Rodríguez - dribeas Mar 05 '14 at 22:42