A function is declared as decltype(auto) f() { return x; } where x is a global int. What does f return?
Correct answer
A. int, because decltype of a bare name yields its declared type
20 views
A. int, because decltype of a bare name yields its declared type
20 views
decltype(auto) deduces the return type from the expression using decltype rules, and decltype of a bare identifier gives that identifier's declared type, here int. Returning (x) with parentheses would yield int& instead, which is the classic trap of the feature. Ordinary auto differs again: it always strips references and top-level const, so it would also give int.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.