以下代码可以使用gcc-4.5.1编译,但无法在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
被映射到std::array<> troll
。插入操作正常,程序可以编译通过。但是,当我尝试使用auto z = x[1]
获取映射到1
的数组troll
时,程序会出现以下错误:error C2512: 'std::array<_Ty,_Size>::array'
: no appropriate default constructor available
是什么原因导致了GCC和VS11之间的这种行为差异,如何解决?
谢谢。
auto z(x[1]);
吗? - n. m.std::array<const unsigned int, 4> another_troll;
也会出现相同的错误。无需使用 map。 - Dan Nissenbaum