samedi 25 février 2017

Is it a good idea to change pre c++11 setting functions to modern templated ones with forwarding

in, my classes I have lots of setXXX function which looks something like follows:

void setName(const std::string& newName) {
   name = newName;
}

this is what most programmers would do before C++11.

I learned (item 25 from Effective Moderm C++) that using forwarding would make those functions more efficient. So they would look as follows:

template<typename T>
void setName(T&& newName) {
   name = std::forward<T>(newName);
}

my qestion is whether it is a good idea to convert all such pre C++11 setting functions to templates with forwarding reference parameter? My current understanding is that it will always be beneficial, at least I dont see drawbacks.

Aucun commentaire:

Enregistrer un commentaire