In Herb Sutter's talk at C++Con 2014, among other things he discusses passing by value, by reference, and so forth. One technique he presents in this albeit contrived example is:
using namespace std;
class employee{
string name_;
public:
template<class String, class = enable_if_t<!is_same<decay_t<String>, string>::value>>
void set_name(String&& name) noexcept(is_nothrow_assignable<string&, String>::value){
name_ = forward<String>(name);
}
};
I understand that String&&
is a universal or forwarding reference as String
is a deduced template type, and that one should therefore use forward<String>(name)
. Whilst I have some very limited experience with template metaprogramming, it is not obvious to me what is the purpose of the unamed tempate parameter, with the enable_if_t...
and would appreciate an explanation to decipher its purpose. How does the noexcept
work? What would have been wrong if one had naively written:
template<class String>
void set_name(String&& name){
name_ = forward<String>(name);
}