For int x = 0; what do decltype(x) and decltype((x)) yield?
Correct answer
C. int and int&, because a parenthesised lvalue expression produces a reference type
12 views
C. int and int&, because a parenthesised lvalue expression produces a reference type
12 views
decltype has two rules: an unparenthesised id-expression yields the declared type, while any other expression yields a reference for lvalues, so decltype((x)) is int&. The distinction matters in forwarding wrappers where the return type must preserve the value category. Combined with auto as decltype(auto), the same rules apply to deduced return types.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.