I am learning std::forward . I wrote a short program to check what happens if we do not call std::forward before passing the arguments to another function call:
#include <iostream> #include <typeinfo> #include <string> using namespace std; class Example { }; ostream &operator << (ostream &os, const Example &e) { os << "yes!"; return os; } void test_forward_inner(const Example &e) { cout << "& " << e << endl; } void test_forward_inner(Example &&e) { cout << "&& " << e << endl; } void test_forward_inner(const string &e) { cout << "& " << e << endl; } void test_forward_inner(string &&e) { cout << "&& " << e << endl; } template <typename T> void test_forward_wrapper(T &&arg) { test_forward_inner(arg); } int main() { Example e; test_forward_wrapper(e); test_forward_wrapper(Example()); cout << endl; string s("hello"); test_forward_wrapper(s); test_forward_wrapper("hello"); return 0; }
Here I tried forwarding lvalue and rvalue from test_forward_wrapper() to test_forward_inner() . Running this program gives the result:
& example & example & hello && hello
For std::string s, the desired internal function was called, but for my own class, only the lvalue version was called. Only if I call std::forward before passing the arguments to the internal function can the rvalue version be called.
What is the difference here? As I know, in accordance with the rules for dropping links, when the shell was called using Example() , the value of rvalue T will be displayed as Example , and arg will be of type Example && , so the rvalue version should be called an internal function.
And for other situations, such as the case of std::string here, the correct version of the internal function was called, then can std::forward be deleted here? If not, what (maybe something bad) will happen?
source share