std::for_each编译错误,使用g++,而不是VS2012
std::for_each compile error with g++, not VS2012
我有以下代码(尽可能简化):
#include <vector>
#include <algorithm>
using std::vector;
enum class Foo {
BAR, BAZ
};
void print_to_file(const vector<const vector<Foo> >& sequences) {
std::for_each(sequences.begin(), sequences.end(), [](const vector<Foo>& sequence) {
});
}
int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
当用g++ (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773]
作为g++ --std=c++11 test.cpp
编译时,我得到以下错误消息:
In file included from /usr/include/c++/4.7/x86_64-suse-linux/bits/c++allocator.h:34:0,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/vector:62,
from test.cpp:1:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘struct __gnu_cxx::new_allocator<const std::vector<Foo> >’:
/usr/include/c++/4.7/bits/allocator.h:89:11: required from ‘class std::allocator<const std::vector<Foo> >’
/usr/include/c++/4.7/bits/alloc_traits.h:89:43: required from ‘struct std::allocator_traits<std::allocator<const std::vector<Foo> > >’
/usr/include/c++/4.7/ext/alloc_traits.h:109:10: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<const std::vector<Foo> > >’
/usr/include/c++/4.7/bits/stl_vector.h:76:28: required from ‘struct std::_Vector_base<const std::vector<Foo>, std::allocator<const std::vector<Foo> > >’
/usr/include/c++/4.7/bits/stl_vector.h:208:11: required from ‘class std::vector<const std::vector<Foo> >’
test.cpp:11:25: required from here
/usr/include/c++/4.7/ext/new_allocator.h:83:7: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const std::vector<Foo>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::vector<Foo>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::vector<Foo>&]’ cannot be overloaded
/usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const std::vector<Foo>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::vector<Foo>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::vector<Foo>&]’
同样的代码在VS2012中编译得很好,但在g++下失败得很厉害,我根本不知道如何解释错误消息。
我很惊讶你在其他地方没有遇到问题,因为这个vector<const vector<Foo> >
不是一种可能的数据类型。
std::vector
的value_type至少必须是可复制的(C++03)或可移动的(C++11)。如果它是常量,则不能分配或移动它。
用于标准容器的分配器要求不允许容器持有const
对象。C++11 17.6.3.5"分配器需求"概述了标准分配器的需求,使用了一组以表27开头的表。在表27中,第一个定义是"描述性变量"T
、U
和C
,它们被定义为"任何非常量、非引用对象类型"。用于分配器类定义的描述性变量X
和Y
为:
X
-类型为T
的分配器类
Y
-类型U
的相应分配器类
总之,标准分配器是根据非常量类型的参数化定义的。
早在2004年,就有一个旧的GCC/libstdc++错误(被解析为无效)(C++2003对在容器中存储常量对象也有类似的限制,但它更容易显示,因为2003标准更直接地规定,存储在容器中的对象类型必须是"可分配的")。
- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16875
相关文章:
- 在 vs2012 中,lambda 中的 noexcept 如何工作?
- VS2012 不编译此代码,但 G++、clang 和 ICC 都接受它而无需诊断
- VS2012:“找不到资源编译器DLL.请确保路径正确.”
- 从VS2008升级后,VS2012中的构建项目显示错误
- OpenCV with VS2012 (Qt Support)
- VS2012 中的 is_directory () 对于偶数目录返回 false
- VS2010 和 VS2012 以及 C++11 错误 C2059:语法错误:"..."怎么走?
- 在VS2012中使用C++11条件变量
- std::remove_pointer 无法删除 VS2012 上的常量易失性函数指针
- 标准::variate_generator 在 VS2012 中
- 从命令行运行本机 VS2012/C++ 64 位单元测试
- 仅运行自定义生成规则 - VS2012
- VS2012 bind2nd 不是 std 的成员
- VS2012 C++ DLL compatability
- Delaunay Trangulation for VS2012 C++?
- 获取线路错误 MFC vs2012 (msvcp110.dll)
- Boost 1_52内部版本VS2012失败
- <filesystem> VS2013 与 VS2012 中的不同行为
- libtiff with VS2012
- 自(vs2012/2010/2013)以来,msvcrt是否使用不同的堆进行分配