C++ auto vs auto&

The type deduction for auto works exactly the same as for templates:

  • when you deduce auto you will get a value type.
  • when you deduce auto& you wil get a non-const reference type
  • when you deduce const auto& you will get a const reference
  • when you deduce auto&& you will get
    • a non-const reference if you assign a non-const reference
    • a const reference if you assign a const reference
    • a value when you assign a temporary

Leave a Comment