C++ 右值引用重载,而不是移动构造函数
c++ rvalue reference overload other than move constructor
有人可以举一个函数重载的例子,说明一个重载了rValue引用参数的函数,而不是移动构造函数或移动赋值运算符,该函数充分利用了传递的参数是右值的事实? 因此,它执行函数的左值参数版本的功能,但除了使复制或赋值更有效之外,它还做了一些特殊的事情(仅仅是因为传递了右值)。 也许使其他一些操作更有效率? 更好的是,也许在某些情况下需要一些额外的东西,但无法用左值执行? 到目前为止,我想不出任何例子。
所以我问的是你自己的函数的重载:
void ping (const Person& person) {
// do something
}
void ping (Person&& person) {
// do what the first ping does but take advantage of the fact that an rvalue (a temporary?) was passed
}
许多其他运算符在标准库中都有右值引用重载,例如,字符串operator+
重载:
template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator>
operator+(const charT* lhs,
basic_string<charT,traits,Allocator>&& rhs);
template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator>
operator+(basic_string<charT,traits,Allocator>&& lhs,
const charT* rhs);
等
当然,push_back
和emplace_back
以及容器中其余的插入/置换函数系列:
void push_back(T&& x);
template <class... Args> void emplace_back(Args&&... args);
相关文章:
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 为什么调用复制构造函数而不是移动构造函数?
- 基类中的默认析构函数禁用子类中的移动构造函数(如果有成员)
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 移动构造函数和右值引用
- 为什么 std::memmove 中联合的默认非平凡移动构造函数C++?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- C++:为什么不调用移动构造函数?
- 移动构造函数永远不会被调用
- C++:关于使用 Stroustrup 示例移动构造函数/赋值的问题
- 运算符+ 的规范实现涉及额外的移动构造函数
- C ++为什么在移动构造函数中需要移动/前进
- 为什么在删除"移动构造函数"时使用"复制构造函数"?
- 为什么这里不调用移动构造函数?
- 隐式移动构造函数
- 如何为具有私有成员的派生类实现移动构造函数
- 为什么不调用移动构造函数
- 是否可以避免在以下代码中复制/移动构造函数的需要?