2

I have struct which contains array of inner struct. I want to use method emplace_back() of vector<my_struct>. But I cannot figure how could I initialize this struct correctly:

struct my_struct
{
    struct
    {
        float x, y, z;
    } point[3];
};

std::vector<my_struct> v;

v.emplace_back(
    {0, 0, 0},
    {0, 0, 0},
    {0, 0, 0}
);

This gives compilation error error: no matching function for call to ‘std::vector<main()::my_struct>::emplace_back(<brace-enclosed initializer list>, <brace-enclosed initializer list>, <brace-enclosed initializer list>)

Is it possible to emplace_back this struct (I'm using C++17)? Should I write custom constructor?

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91

1 Answers1

3

how about this:

v.push_back(my_struct{{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}});
coldmund
  • 155
  • 1
  • 10
  • Yes, `v.push_back()` is working, but it is possible with `emplace_back()`? – Andrej Kesely May 31 '19 at 13:45
  • @AndrejKesely It is the same thing: http://coliru.stacked-crooked.com/a/14b2087f25634e90 – Amadeus May 31 '19 at 13:47
  • @Amadeus I see, push_back() is working even like this `v.push_back({{{0, 0, 0},{0, 0, 0},{0, 0, 0}}});`, in emplace_back() I need to specify `my_struct` first: `v.emplace_back(my_struct{{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}});` Why? – Andrej Kesely May 31 '19 at 13:51
  • 2
    @AndrejKesely Maybe this explain for you: https://stackoverflow.com/a/43395138/2293156 – Amadeus May 31 '19 at 14:17