c++ - Visual C++ 为什么 std::move 崩溃

标签 c++ c++11 visual-c++ stdvector

我想将一个 vector 附加到另一个 vector 的末尾。据我所知,函数 std::move() 是此任务的“首选函数”。为什么 Microsoft Visual C++ Express 中的 std::move() 会崩溃,而手工制作的循环却按预期工作?

我正在使用 Microsoft Visual C++ 2015 Update 3。不幸的是,我无法使用其他编译器对此进行测试。

// The setup code for the two vectors:
vector<unique_ptr<channel>> channels, added_channels;

// ...  here is some code that adds some elements to both vectors

据我所知,以下两段代码应该以相同的方式工作。他们应该将 added_channels 的元素移动到 channels 的末尾。

这是第一个崩溃的变体:

std::move(added_channels.begin(), added_channels.end(), channels.end());

这是第二个有效的版本:

for(auto & ref : added_channels)
{
    channels.push_back(std::move(ref));
}

最佳答案

std::move 移动到特定位置。
如果你想将它插入到 vector 的后面,你应该使用 std::back_inserter

std::move(added_channels.begin(), added_channels.end(),  std::back_inserter(channels));

关于c++ - Visual C++ 为什么 std::move 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48381058/

相关文章:

c++ - 从初始化列表初始化,但没有 {{{{{{{{ ... }}}}}}}}?

c++ - 使用相同的函数但不同的重载来转换 std::tr1::shared_ptr<T> 和 std::shared_ptr<T>

c++ - 左移的一些意外行为 <<

c++ - c++宏重新定义的范围是什么?

python - 如何防止 Python 返回到 C++ boost::python::error_already_set?

c++11 - Boost asio thread_pool join 不等待任务完成

c++ - 排序列表与未排序列表的线性搜索——为什么排序更慢?

c++ - QDoubleValidator 和 QLineEdit onEditFinished 冲突?

c++ - 带链表的哈希表,重复节点仍在保存(C++)

c++ - 定义类