In Scott Meyer's book, Effective Modern C ++, we can read that:
std::vector<bool> features(const Widget& w); Widget w; β¦ bool highPriority = features(w)[5]; β¦ processWidget(w, highPriority);
and parameter with auto
auto highPriority = features(w)[5];
which causes undefined behavior, because features() returns std::vector<bool> , which uses an Object proxy of type std::vector<bool>::reference when returning values ββfrom opearator[] .
As a solution to this, it is recommended not to stop using auto , but to use static_casts .
So Scott Meyers advises using:
auto highPriority = static_cast<bool>(features(w)[5]);
instead:
bool highPriority = features(w)[5];
My question is: What is the difference between the two? In my opinion, both of them are the same, because both methods make refactoring more difficult in exactly the same way (changing the type of the return value in the functions of the function does not make the highPriority variable another type), and the second is shorter for writing.
source share