简体   繁体   English

为什么 make_move_iterator() 不能与 back_inserter() 一起使用?

[英]Why make_move_iterator() cannot work with back_inserter()?

I'm trying to compile simple piece of code under C++17 standard:我正在尝试在 C++17 标准下编译一段简单的代码:

#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

int main() {
    vector<int> numbers = {6, 1, 3, 9, 1, 9, 8, 12, 1};
    vector<int> res;

    copy(begin(numbers), end(numbers), make_move_iterator(back_inserter(res)));

    return 0;
}

I get an error on make_move_iterator(back_inserter(res)) .我在make_move_iterator(back_inserter(res))上遇到错误。

Beginning of error is (excluding senseless (in my opinion) subsequent information):错误的开始是(不包括毫无意义的(在我看来)后续信息):

In file included from C:/Users/Name/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:67,
                 from C:/Users/Name/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/algorithm:61,
                 from C:\Users\Name\CLionProjects\coursera_cpp_course3\week5\c3_w5_t6_merge_sort_3\c3_w5_t6_merge_sort_3.cpp:2:
C:/Users/Name/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h: In instantiation of 'class std::move_iterator<std::back_insert_iterator<std::vector<int> > >':
C:\Users\Name\CLionProjects\coursera_cpp_course3\week5\c3_w5_t6_merge_sort_3\c3_w5_t6_merge_sort_3.cpp:49:77:   required from here
C:/Users/Name/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_iterator.h:1023:24: error: forming reference to void
     __base_ref>::type  reference;
                        ^~~~~~~~~

I've read documentation on make_move_iterator() , and haven't seen any preconditions on use of it here.我已经阅读 了有关 make_move_iterator() 的文档,但在这里没有看到使用它的任何先决条件。

Why does it happen?为什么会这样? Thank you for your suggestions.谢谢你的建议。

Conditions under which I try to compile:我尝试编译的条件:

  • Windows 10 Windows 10

  • MinGW 8.1.0 MinGW 8.1.0

  • CMake 3.17.1 CMake 3.17.1

Update: I understood my fault.更新:我理解我的错。 Here should be:这里应该是:

copy(make_move_iterator(begin(numbers)), make_move_iterator(end(numbers)), back_inserter(res));

std::make_move_iterator contructs an std::move_iterator , which is suppoed to work with an InputIterator , ie to move the values from the iterator instead of copying them. std::make_move_iterator构造一个std::move_iterator ,它应该与InputIterator一起工作,即从迭代器中移动值而不是复制它们。

std::move_iterator is an iterator adaptor which behaves exactly like the underlying iterator ( which must be at least an LegacyInputIterator ), except that dereferencing converts the value returned by the underlying iterator into an rvalue. std::move_iterator是一个迭代器适配器,其行为与底层迭代器完全相同(必须至少是LegacyInputIterator ),除了取消引用将底层迭代器返回的值转换为右值。 If this iterator is used as an input iterator, the effect is that the values are moved from, rather than copied from .如果将此迭代器用作输入迭代器,则效果是从 中移动值,而不是从 中复制值

The document you linked to clearly says:您链接到的文件清楚地说:

Parameters参数

i - input iterator to be converted to move iterator i - 要转换为移动迭代器的输入迭代

std::back_insert_iterator is not an input iterator , it is an output iterator . std::back_insert_iterator不是输入迭代器,它是output 迭代器 The value_type of std::back_insert_iterator is void , which is what the error message is complaining about: std::back_insert_iteratorvalue_typevoid ,这是错误消息所抱怨的:

forming reference to void形成对 void 的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM