带有std::move和std::make_move_iterator的C++11编译错误
C++11 compilation errors with std::move and std::make_move_iterator
有人能向我解释一下为什么编译失败吗:
#include <iterator>
#include <iostream>
#include <unordered_set>
#include <utility>
#include <set>
template<typename T>
std::unordered_set<T> FailMove(std::set<T> &&set) {
std::unordered_set<T> response;
response.insert(std::make_move_iterator(set.begin()),
std::make_move_iterator(set.end()));
return response;
}
int main(int argc, char **argv) {
std::set<int> set{1, 3, 5, 7};
auto res = FailMove(std::move(set));
std::cout << res.size() << 'n';
return 0;
}
clang输出(命令:clang++ -std=c++11 -otest test.cpp
)为:
In file included from test.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:948:14: error: cannot
cast from lvalue of type 'const value_type' (aka 'const int') to rvalue reference type 'reference' (aka 'int &&'); types are not
compatible
return static_cast<reference>(*__i);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_set:830:34: note: in
instantiation of member function 'std::__1::move_iterator<std::__1::__tree_const_iterator<int, std::__1::__tree_node<int, void *> *,
long> >::operator*' requested here
__table_.__insert_unique(*__first);
^
test.cpp:10:12: note: in instantiation of function template specialization 'std::__1::unordered_set<int, std::__1::hash<int>,
std::__1::equal_to<int>, std::__1::allocator<int> >::insert<std::__1::move_iterator<std::__1::__tree_const_iterator<int,
std::__1::__tree_node<int, void *> *, long> > >' requested here
response.insert(std::make_move_iterator(set.begin()),
^
test.cpp:18:14: note: in instantiation of function template specialization 'FailMove<int>' requested here
auto res = FailMove(std::move(set));
^
1 error generated.
gcc输出(命令:g++ -std=c++11 -otest test.cpp
):
In file included from /usr/include/c++/4.8/iterator:63:0,
from test.cpp:1:
/usr/include/c++/4.8/bits/stl_iterator.h: In instantiation of 'std::move_iterator<_Iterator>::value_type&& std::move_iterator<_Iterator>::operator*() const [with _Iterator = std::_Rb_tree_const_iterator<int>; std::move_iterator<_Iterator>::reference = int&&; std::move_iterator<_Iterator>::value_type = int]':
/usr/include/c++/4.8/bits/hashtable_policy.h:647:18: required from 'void std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::move_iterator<std::_Rb_tree_const_iterator<int> >; _Key = int; _Value = int; _Alloc = std::allocator<int>; _ExtractKey = std::__detail::_Identity; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, true, true>]'
/usr/include/c++/4.8/bits/unordered_set.h:393:4: required from 'void std::unordered_set<_Value, _Hash, _Pred, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::move_iterator<std::_Rb_tree_const_iterator<int> >; _Value = int; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<int>]'
test.cpp:10:3: required from 'std::unordered_set<T> FailMove(std::set<T>&&) [with T = int]'
test.cpp:18:37: required from here
/usr/include/c++/4.8/bits/stl_iterator.h:963:37: error: invalid initialization of reference of type 'std::move_iterator<std::_Rb_tree_const_iterator<int> >::reference {aka int&&}' from expression of type 'std::remove_reference<const int&>::type {aka const int}'
{ return std::move(*_M_current); }
然而,这段代码在两个编译器中都能编译而没有问题:
#include <iterator>
#include <iostream>
#include <unordered_map>
#include <utility>
#include <map>
template<typename K, typename V>
std::unordered_map<K, V> FailMove(std::map<K, V> &&map) {
std::unordered_map<K, V> response;
response.insert(std::make_move_iterator(map.begin()),
std::make_move_iterator(map.end()));
return response;
}
int main(int argc, char **argv) {
std::map<int, int> map{{1, 1}, {3, 3}, {5, 5}, {7, 7}};
auto res = FailMove(std::move(map));
std::cout << res.size() << 'n';
return 0;
}
clang版本测试:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
测试的gcc版本:
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
短版本为set::begin()
返回const_iterator
,而map::begin()
返回iterator
。您不能从const_iterator
移动。
长版本是关联容器的"Key"组件在容器中被视为const
。set
只包含一个Key组件。map
包含Key组件和Value组件。set
的值是Keys。map
的值为std::pair< const Key, Value >
。
这是因为以改变元素顺序的方式修改标准容器的Key组件会破坏容器的不变量。即使你打算很快放弃它,这也是真的,因为即使是遍历、销毁或其他任何东西都可以通过编辑关键组件来破坏(理论上)!
当您从迭代器it
移动时,它会尝试将*it
强制转换为value_type&&
。对于常量迭代器,*it
返回value_type const&
,强制转换失败。
在map
的情况下,移动将移动Value组件,并复制Key组件。
相关文章:
- Usages of std::move
- 在C++中对T*类型执行std::move的意外行为
- 关于std::move的使用,是否有编译警告
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 通过实例理解std::move及其目的
- 返回一个带有 std::move 的对象并链接函数
- '[](std::list& list)<int>{return std::move(list)}(list)' 是否保证将 'list' 留空?
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- 为什么当我为 for(auto& it : myUnorderedMap) {... = std::move(it.second)} 时,我会得到一个 const 引用?
- std::move a const std::vector in a lambda capture
- "std::forward"和"std::move"真的不生成代码吗?
- std::vector move 而不是交换到空 vector 并释放存储
- 是否有必要使用 std::move?这不是已经是一个右值参考了吗?
- 如何在没有 std::move 的情况下移动临时对象
- 关于在成员重载中使用 std::move() 的问题
- 显式清除 std::move 之后的源资源
- std::move(key) 同时迭代unordered_map<字符串,字符串>?
- std::move(std::array) g++ vs visual-c++
- 使用 move std::string 的 .data() 成员不适用于小字符串?
- std::move(std::unique_ptr()) 组合是否有标准缩写