When creating local variables, is it correct to use (const) auto& or auto?
e.g.:
SomeClass object;
const auto result =
Yes, it is correct to use auto and auto& for local variables.
When getting the return type of a function, it is also correct to use auto&. This applies for range based for loops as well.
General rules for using auto are:
auto x when you want to work with copies. auto &x when you want to work with original items and may modify them. auto const &x when you want to work with original items and will
not modify them.You can read more about the auto specifier here.