【问题标题】:C++11 compilation errors with std::move and std::make_move_iteratorstd::move 和 std::make_move_iterator 的 C++11 编译错误
【发布时间】:2015-09-12 03:01:24
【问题描述】:

谁能解释一下为什么编译失败:

#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.

【问题讨论】:

  • 在 Visual Studio 2013 中对我来说失败了,很奇怪。错误 C2440: 'return' : cannot convert from 'const int' to 'int &&'" 这是我得到的一系列错误之一。编辑:你的第二个例子编译得很好,正如预期的那样。
  • @Robinson 如果您阅读了错误消息,那正是 clang 中的错误。不太确定 gcc。
  • 您只是好奇,还是真的希望将值移出set?我可以告诉你为什么它不能工作(试图将对象移出std::set)比它不能编译的原因要容易得多:因为set不会让你在数据在容器中时修改它的内容,并且移动迭代器仍然对容器内的数据进行操作。证明它应该导致构建失败更加困难,并且需要进行标准研究。

标签: c++ c++11 dictionary set move-semantics


【解决方案1】:

简短版本是set::begin() 返回一个const_iteratormap::begin() 返回一个iterator。您不能从 const_iterator 移动。

长版本是关联容器的“Key”组件在容器内被视为constset 只包含一个 Key 组件。 map 包含一个 Key 组件和一个 Value 组件。 set 的值是 Keys。 map 的值为std::pair&lt; const Key, Value &gt;

这是因为以改变元素顺序的方式修改标准容器的 Key 组件会破坏容器的不变量。即使您打算立即丢弃它也是如此,因为即使是遍历、破坏或其他任何东西都可以通过编辑 Key 组件(理论上)被破坏!


当您从迭代器 it 移动时,它会尝试将 *it 强制转换为 value_type&amp;&amp;。对于 const 迭代器,*it 返回value_type const&amp;,并且强制转换失败。

map的情况下,移动会移动Value组件,并复制Key组件。

【讨论】:

  • 看起来move_iterator::referenceLWG 2106 中从基于value_type 更改为基于reference; OP 中的代码使用 GCC 5.1 编译。
  • @T.C.啊:它可以编译,但它“无法工作”,因为所谓的移动迭代器不会移动(除非值类型具有可变状态和 const&amp;&amp; 移动构造函数)。这看起来确实是“正确的做法”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多