Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 11 compile-time 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 is now mapped to std::array<> troll. The insertion works well and the program compiles. But, as soon as i try auto z = x[1] -> Therefore trying to get the array troll that 1 is mapped to, the program does not compile with the following error:

error C2512: 'std::array<_Ty,_Size>::array' : no appropriate default constructor available

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

Thanks.

like image 624
ScarletAmaranth Avatar asked May 06 '12 00:05

ScarletAmaranth


1 Answers

Try auto z = *x.find(1); instead. The []-operator requires a default-constructible type. In fact, the entire container requires a default-constructible type, so you really can't expect anything but random luck as you try out various implementations.

like image 156
Kerrek SB Avatar answered Oct 13 '22 10:10

Kerrek SB