I have heard about “Perfect Forwarding” recently (from Scott Meyers Effective modern C++) like,
Case 1. Not using Perfect Forward
class Widget {
public:
void setName(const std::string& newName) { name = newName; }
void setName(std::string&& newName) { name = std::move(newName); }
...
Case 2. Using Perfect Forward
class Widget {
public:
template<typename T>
void setName(T&& newName) { name = std::forward<T>(newName); }
...
So now, I know there are lots of advantage of using perfect forwarding. but in case 1, the user knows the parameter type of setName
is std::string
. However, in case 2, it can't.
So my questions are:
- When using perfect forwarding, how user know what parameter type to pass?
- How to overcome this disadvantage?