简体   繁体   中英

c++ rvalue reference overload other than move constructor

Can someone give an example of a function overloaded with an rValue reference parameter other than a move constructor or move assignment operator that makes full use of the fact that the argument passed is an rvalue? Thus it carries out the functionality of the lvalue-parameter version of the function but also does something special in addition (solely because an rvalue is being passed), other than making a copying or assignment more efficient. Perhaps rendering some other action more efficient? Even better, perhaps something extra that is needed in some situations but could not be carried out with an lvalue? So far I cannot think of any example.

So I'm asking about an overload of your own function:

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 
}

Plenty of other operators have rvalue-reference overloads in the standard library, for example, operator+ for strings:

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);

etc

And of course, push_back and emplace_back and the rest of insert/emplace families of functions in the containers:

void push_back(T&& x);
template <class... Args> void emplace_back(Args&&... args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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