14

I have this odd problem

    vector<unsigned int[3]> tris;
    for (unsigned int i = 0; i < idx.size() - 2; i++) {
        unsigned int push[] = {idx[i], idx[i+1], idx[i+2]};
        tris.push_back(push); //<- this is where it goes belly up
    }

The code piece is supposed to unravel a triangle strip index list into triangle indices but wont compile under vs10. Thoughts?

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26

3 Answers3

24

No, unless you wrap your arrays into a struct or use something like std::array.

The naked C-style array type is not copyable or assignable, which makes it ineligible to serve as a standard container element.

P.S. It should be noted though that C++11 switched to a more elaborate (per-method) approach to specifying requirements to container element type, instead of the more broad approach used by C++03. The above ineligibility claim is based on C++03. I'm not ready to say that it is so unconditionally true for C++11 as well... But it is certainly true even in C++11 if you insist on using push_back in your code.

P.P.S. In C++11 one can probably get away with std::list of naked C-style arrays and using emplace to construct new elements. But not with std::vector.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
12

Use std::vector<std::array<unsigned, 3>> instead.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
  • This answer looks like it works, but it doesn't. I mean, it works because it lays out everything in a flat contiguous sequence of bytes. But you cannot do `push_back(arr)` on it and hoping that it will append the new `arr`. But you can use `std::memcpy`. – daparic Jun 21 '20 at 22:20
  • In C++11 you can actually write tris.push_back(std::array{idx[i], idx[i+1], idx[i+2]}); Combined with the fact that tris.data() is the equivalent C-style array, makes this choice the closest to the original intention, in my opinion. – MikeLima May 08 '22 at 10:55
0

If you are using C++11 you can use tuples.

std::vector < std::tuple< unsigned int, unsigned int, unsigned int > > tris;

https://stackoverflow.com/a/15734156/846686

A less 'elegant' solution can be a pair of a pair

    std::vector < std::pair< unsigned int, std::pair<unsigned int, unsigned int> > tris;

but it may result in a very confused code to read...

Community
  • 1
  • 1
campisano
  • 229
  • 2
  • 10