const className& f();
If I have a function that return const ref. And use it's value to assign to a variable using auto
auto v1 = f();
auto& v2 = f();
const auto& v3 = f();
decltype(auto) v4 = f();
Are v1
, v2
, v3
and v4
actually all the same type? If so, which option is preferred?
No, they are not the same
static_assert(std::is_same_v<decltype(v1), className>);
// plain auto does not deduce reference or apply const
static_assert(std::is_same_v<decltype(v2), const className&>);
// v2 will also be const& as function returns const&
static_assert(std::is_same_v<decltype(v3), const className&>);