为什么我不需要 std::move to std::bind'ed 函数?
Why don't I need to std::move to a std::bind'ed function?
假设我有一个函数,取一个右值引用:
void whatever (std::unique_ptr<int>&&) {
// Nothing!
}
我把它的一个参数绑定到一个占位符上。
auto f = std::bind(&whatever, _1);
我尝试了这样的调用,结果与我预期的相反。
std::unique_ptr<int> nothing;
f(std::move(nothing)); // Fails to compile!
f(nothing); // Works, but seems wrong!
这是编译器错误吗?或者,工作调用是否是不安全的代码?或者为什么我不必将这个指针std::move
放入绑定函数中?
顺便说一下,gcc4.4的编译错误是:
test.cxx:14: error: no match for call to '(std::_Bind<void (*(std::_Placeholder<1>))(std::unique_ptr<int, std::default_delete<int> >&&)>) (std::unique_ptr<int, std::default_delete<int> >)'
使用libc++时,我得到的结果正好相反。
std::unique_ptr<int> nothing;
f(std::move(nothing)); // Works!
f(nothing); // Fails to compile!
我认为这是一个gcc4.4错误。[func.bind.bind]/p10/b3描述了这种情况:
- 如果
is_placeholder<TiD>::value
的值j
不为零,则变元为std::forward<Uj(uj)>
,其类型Vi
为Uj&&
这可能会在最近的gcc中得到修复(我不知道)。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 我应该移动 std::exchange ed 成员吗?
- 调用函数,该函数是 std::bind(ed) 与 std::ref 参数
- 使用C++0x std::thread调用新ed对象中的成员函数
- 为什么我不需要 std::move to std::bind'ed 函数?
- std::revers(ed) std::string是否在开始处包含空字符?