我正在使用来自存储库的最新可用 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));
}
请注意,当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&)’
此外,这应该可以工作,因为已经修复了类似的错误[C++0x] 在关联和无序容器中实现 emplace*。
也许我做错了什么?在报告之前,我想确定这是 GCC 或 libstdc++ 错误。