Visual Studio 11 compilation error using std :: map

The following code compiles with gcc-4.5.1, but not in Visual Studio 11.

#include <map> #include <array> typedef std::pair<const unsigned int, std::array<const unsigned int, 4>> pairus; int main(){ std::map<const unsigned int, std::array<const unsigned int, 4> > x; std::array<const unsigned int, 4> troll = {1, 2, 3, 4}; x.insert(pairus(1, troll)); auto z = x[1]; } 

1 now maps to std::array<> troll . The insert works well and the program compiles. But, as soon as I try auto z = x[1] → Therefore, trying to get the troll array to which 1 attached, the program does not compile with the following error:

error C2512: 'std::array<_Ty,_Size>::array' : there is no suitable default constructor

What causes this difference in behavior between gcc and vs11 and how to fix it?

Thanks.

+6
source share
2 answers

Try auto z = *x.find(1); . [] operator requires a default type. In fact, the entire container requires a default built type, so you really can't expect anything but random luck when trying to implement various implementations.

+4
source

Your type is not assigned because it contains constants.

x [1] is trying to return a link that may be assigned. It will also construct an empty value for the key, if it does not already exist. Both of these types are not valid with your type. You will need to use find.

+3
source

Source: https://habr.com/ru/post/914994/


All Articles