0

The following (contrived example) does not compile in MSVC with /std:c++latest (tested in VS 2019 16.10.3):

#include <vector>

int main() {
    std::vector<int[2]> v(2);
}

failing with C2440: 'return': cannot convert from int * to _Ty (*) (with _Ty=int [2], in a placement new statement, for context).

Is this a known bug? Is there a drop-in workaround without falling back to an old standard? (vector::resize doesn't work either.)

YiFei
  • 1,752
  • 1
  • 18
  • 33
  • 2
    `std::array` as the element type would work. – HolyBlackCat Aug 14 '21 at 17:49
  • `std::vector` cannot operate with array types like `int[2]`. Wrap `int[2]` with a struct/class to make it work. Or use `std::array` instead of `int[]` – ALX23z Aug 14 '21 at 17:49
  • 1
    I've done some reading, and it looks like this actually is not supposed to work. [(1)](https://en.cppreference.com/w/cpp/container/vector) *"Generally, it is required that element type meets the requirements of Erasable"*, [(2)](https://en.cppreference.com/w/cpp/named_req/Erasable) *"[erasable] is equivalent to the validity of `p->~T()`"* [(3)](http://eel.is/c++draft/expr.prim.id.dtor#:expression,pseudo-destructor_call) *"[in] a pseudo-destructor [call], `T` shall be a scalar type"*. Since arrays are not scalar types, `int[2]` is not erasable, so vectors can't hold it. – HolyBlackCat Aug 14 '21 at 17:55

0 Answers0