我正在使用存储库中最新的可用GCC构建.我决定使用它,因为一些额外的C++ 0x功能.然而现在我坚持使用什么假设工作 - 我想通过r值添加新的元素来映射.简化代码,演示了问题:
#include <tr1/unordered_map>
class X
{
public:
X (void) { /* ... */ };
X (const X& x) = delete;
X (X&& x) { /* ... */ };
};
int main (void)
{
std::tr1::unordered_map<int, X> map;
// using std::tr1::unordered_map<int, X>::value_type didn't help too
std::pair<int, X> value (1, X ());
map.insert (std::move (value));
}
Run Code Online (Sandbox Code Playgroud)
请注意,当X使用某些原始类型替换类时,如int代码编译并且工作正常.
在我的生产中,对应于X的代码类也没有复制构造函数.
错误消息(像所有与模板相关的错误)冗长且不可读,我不确定将它放在这里是否是个好主意.如果您想要错误消息,请通知我,因此我将更新此问题.消息的最后部分很有趣:
(...)
/usr/include/c++/trunk/ext/new_allocator.h:106:9: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’
In file included from /usr/include/c++/trunk/utility:71:0,
from /usr/include/c++/trunk/tr1/unordered_map:34,
from kod.cpp:1:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int, _T2 = X, std::pair<_T1, _T2> = std::pair<const int, X>]’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/trunk/bits/stl_pair.h:110:17: error: use of deleted function ‘X::X(const X&)’
Run Code Online (Sandbox Code Playgroud)
此外,这应该工作,因为已经修复了类似的错误[C++ 0x]在关联和无序容器中实现emplace*.
也许我做错了什么?我想确定,在报告之前,这是GCC或libstdc ++错误.
除了使用tr1之外,您的代码对我来说是正确的.tr1限定的东西不知道rvalue引用或移动语义.
我接受了你的代码,从头文件和命名空间限定符中删除了tr1,并使用g ++ - 4.4和libc ++(http://libcxx.llvm.org/)成功编译了代码.尝试删除tr1.
| 归档时间: |
|
| 查看次数: |
1374 次 |
| 最近记录: |