C++ template auto return type with arrow-notation require decltype?

  • A+
Category:Languages

C++11 introduced the arrow-notation(don't know the name) for return types in functions:

template <typename T> auto fun(T&& a) -> decltype(bar(a)){ ... } 

but according to scott meyer, using auto as the return type by itself will drop all const and reference qualifiers(because it follows the same pattern as template deduction), and therefore the idiomatic way is to do decltype(auto) to keep all qualifiers on top of the type.

However, in this context, is auto inferred to be decltype(bar(a))? Then would decltype(auto) be decltype(decltype(bar(a)))? Would that be redundant?

 


When you have a trailing return type, the auto keyword appears purely as an element of notation. The return type becomes whatever type comes after the ->. No type deduction is performed. This is simply how function declarations with a trailing return type are written. Auto return type deduction only happens when you do not have a trailing return type, i.e., auto is used as a placeholder return type with no -> … at the end of your function declarator.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: