1

I wondered that such simple program can not be compiled with gcc, clang and msvc:

#include <array>
#include <vector>

int main()
{
    std::vector<std::array<int, 3>> v;
    v.emplace_back( 1, 2, 3 );
    return 0;
}

Why do std::array can not be constructed from sequence of values? It looks pretty easy to write custom ctor with variadic universal references to effectively construct std::array from values.

I think it would be nice if std::array was considered a POD-structure.

Ivan Ivlev
  • 332
  • 1
  • 9
  • @ShadowRanger The linked dupe doesn't answer the question at all. – Evg Jun 21 '21 at 12:02
  • 1
    @Evg: What does it not answer? It says precisely what the problem is and why it can't be made to work (it's an aggregate with no user-defined constructors). It covers the obvious alternative attempt (passing `{1, 2, 3}`, which also doesn't work). The only question here besides those is basically "why doesn't the C++ standards committee do what I want?" which isn't a useful question (hovering somewhere between opinion-based and a defect report, neither of which below on StackOverflow). – ShadowRanger Jun 21 '21 at 12:07
  • The question suggests that OP already knows that. The real question is why `std::array` doesn't have a variadic constructor that would made `emplace_back` work. The dupe doesn't answer this question. Whether it is a useful question is a completely different story. Questions like "why doesn't the C++ standards committee do what I want?" are not always opinion-based. Far from. – Evg Jun 21 '21 at 12:11
  • *"I think it would be nice if std::array was considered a POD-structure."* That is actually the issue... `std::array arr(1, 2, 3);` isn't valid. – Jarod42 Jun 21 '21 at 12:14
  • @Jarod42: POD isn't directly relevant there is it? [`std::array` *can* be POD](https://stackoverflow.com/q/3674247/364696) (if no extensions to standard implemented), but even if it was, `std::array arr(1, 2, 3);` wouldn't apply. It's a wrapper around `int[3]`, not around three `int`s; `std::array arr({1, 2, 3})` and `std::array arr{1, 2, 3}` are legal on GCC, but `emplace_back({1, 2, 3})` doesn't work because braced initializers don't work through function calls, which use `std::initializer_list` instead of aggregate init, and only when the receiving type supports it. – ShadowRanger Jun 21 '21 at 23:06
  • @ShadowRanger: `std::array` doesn't have constructor, and use aggregate initialization, and `emplace_back` and other construct in-place doesn't use `{/*..*/}` to construct object, but `(/*..*/)`. and `{/*..*/}` has no type, so cannot be deduced for `emplace_back(Ts&&...)`. – Jarod42 Jun 22 '21 at 07:42

1 Answers1

-3

You are trying to put integers into the std::vector, not std::array<int, 3> as you defined. It is not clear what v.emplace_back( 1, 2, 3 ); is meant to do. Does it mean std::array<int, 3>{ 1, 2, 3 } as one item in the vector or does it mean { std::array<int, 3>{ 1, 1, 1 }, std::array<int, 3>{ 2, 2, 2 }, std::array<int, 3>{ 3, 3, 3 } } or something else?

Daniel Dearlove
  • 565
  • 2
  • 12