C++;11与g+相关的编译错误+;4.6和boost::无序的_图 我有一些C++代码,它编译错误,除非它是在C++ 11模式下运行的,但是我不能理解为什么会出现这种情况,因为代码没有(明确地)使用C++ 11的特性: #include <vector> #include <map> #include <boost/unordered_map.hpp> struct SomeStruct { boost::unordered_map<int, int> intMap; }; int main(int argc, const char* argv[]) { std::vector<SomeStruct> vals; vals.resize(100); }

C++;11与g+相关的编译错误+;4.6和boost::无序的_图 我有一些C++代码,它编译错误,除非它是在C++ 11模式下运行的,但是我不能理解为什么会出现这种情况,因为代码没有(明确地)使用C++ 11的特性: #include <vector> #include <map> #include <boost/unordered_map.hpp> struct SomeStruct { boost::unordered_map<int, int> intMap; }; int main(int argc, const char* argv[]) { std::vector<SomeStruct> vals; vals.resize(100); },c++,gcc,boost,stl,c++11,C++,Gcc,Boost,Stl,C++11,然后我得到了这个编译错误: In file included from /usr/include/c++/4.6/vector:61:0, from test.cpp:1: /usr/include/c++/4.6/bits/stl_algobase.h: In function 'typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__ty

然后我得到了这个编译错误:

In file included from /usr/include/c++/4.6/vector:61:0,
                 from test.cpp:1:
/usr/include/c++/4.6/bits/stl_algobase.h: In function 'typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type std::__fill_a(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = SomeStruct*, _Tp = SomeStruct, typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type = void]':
/usr/include/c++/4.6/bits/stl_algobase.h:722:7:   instantiated from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = SomeStruct*, _Tp = SomeStruct]'
/usr/include/c++/4.6/bits/vector.tcc:397:5:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_fill_insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = SomeStruct, _Alloc = std::allocator<SomeStruct>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<SomeStruct*, std::vector<SomeStruct> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = SomeStruct*, std::vector<_Tp, _Alloc>::size_type = long unsigned int, std::vector<_Tp, _Alloc>::value_type = SomeStruct]'
/usr/include/c++/4.6/bits/stl_vector.h:944:9:   instantiated from 'void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = SomeStruct, _Alloc = std::allocator<SomeStruct>, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<SomeStruct*, std::vector<SomeStruct> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = SomeStruct*, std::vector<_Tp, _Alloc>::size_type = long unsigned int, std::vector<_Tp, _Alloc>::value_type = SomeStruct]'
/usr/include/c++/4.6/bits/stl_vector.h:632:4:   instantiated from 'void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type, std::vector<_Tp, _Alloc>::value_type) [with _Tp = SomeStruct, _Alloc = std::allocator<SomeStruct>, std::vector<_Tp, _Alloc>::size_type = long unsigned int, std::vector<_Tp, _Alloc>::value_type = SomeStruct]'
test.cpp:12:18:   instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:676:2: error: no match for 'operator=' in '* __first = __value'
/usr/include/c++/4.6/bits/stl_algobase.h:676:2: note: candidate is:
test.cpp:5:8: note: SomeStruct& SomeStruct::operator=(SomeStruct&)
test.cpp:5:8: note:   no known conversion for argument 1 from 'const SomeStruct' to 'SomeStruct&'
有人能解释一下为什么这只能在C++11模式下工作吗

编辑:

使用Vagrant复制的步骤:

vagrant init precise64
vagrant up
vagrant ssh
sudo apt-get install -y build-essential libboost1.48-all-dev
echo "#include <vector>
#include <map>
#include <boost/unordered_map.hpp>

struct SomeStruct {
  boost::unordered_map<int, int> intMap;
};

int main(int argc, const char* argv[]) {
  std::vector<SomeStruct> vals;

  vals.resize(100);
}" > test.cpp
g++ test.cpp
vagrant init precise 64
流浪汉
流浪汉
sudo apt get安装-y build-essential libboost1.48-all-dev
echo“#包括
#包括
#包括
结构SomeStruct{
boost::无序映射intMap;
};
int main(int argc,const char*argv[]{
std::向量VAL;
vals.resize(100);
}“>test.cpp
g++test.cpp

boost::unordered_map
的复制赋值运算符声明为:

unordered_map& operator=(unordered_map &t)
SomeStruct &operator= (SomeStruct &);
这又导致
SomeStruct
的隐式复制赋值运算符(C++98 12.8[#10])声明为:

unordered_map& operator=(unordered_map &t)
SomeStruct &operator= (SomeStruct &);
添加显式副本分配运算符

SomeStruct &operator= (const SomeStruct &x) { intMap = x.intMap; return *this; }
解决了这个问题,它通过调用另一个重载
unordered\u map::operator=
来工作

此问题在Boost 1.52中已修复。声明运营商为:

unordered_map& operator=(unordered_map const& x);

我无法在gcc 4.6.3或4.8、ubuntu 12.04 64位和boost 1.46上重现这一点。在MinGW下,使用gcc 4.7.0和boost 1.49,使用
-std=c++03
编译,没有错误。抱歉,这是使用boost 1.48而不是1.46。使用Vagrant进行复制的步骤:
Vagrant init precise64;流浪者;流浪汉;sudo-apt-get-install-y构建基本libboost1.48-all-dev;echo“#include#include#include struct SomeStruct{boost::无序映射intMap;};int main(int argc,const char*argv[]){std::vector vals;vals.resize(100)}”>test.cpp;g++test.cpp
尝试注释掉
std::vector::resize
行。@bamboon注释掉resize行后,代码可以编译。您的显式副本分配是否会编译?intMap=x.intMap应该失败-x.intMap在这里是常量,因此找不到无序映射的副本分配的适当重载。@Basilevs,存在另一个重载
操作符=
-
无序映射和操作符=(const::boost::rv&x)
,我在上面提到过。@chill:谢谢,添加复制分配操作符非常有效!