C++ Don'ts

Undefine Behaviour

return for [[noreturn]]

source

[[noreturn]] constexpr int DontDoThis() { return 42;}
constexpr auto Execute() { return DontDoThis();}
std::cout << "Execute " << Execute() << std::endl; // segmentation fault

Bad Practice

Disable Return Value Optimization (RVO or Named RVO)

source: Item 25: Effective Modern C++

Widget makeWidget()
{
   Widget w; 

   return std::move(w); // NRVO as it's named local variable
}
Object SomeFunction()
{
    Object object1;
    Object object2;
    return condition() ? object1: object2
// this would disable named return value optimization
// this is lvalue
}
  • Do this instead

    if (condition())
    {
        return object1;
    }
    else
    {
        return object2;
    }

Last updated