1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A function is declared as decltype(auto) f() { return x; }…

Added 30 Nov -0001

A function is declared as decltype(auto) f() { return x; } where x is a global int. What does f return?

  1. int, because decltype of a bare name yields its declared type Correct answer
  2. int&, because decltype(auto) gives an lvalue reference
  3. int&&, because returning a global binds it to an rvalue reference
  4. const int, since the returned object counts as a temporary value

Correct answer

A. int, because decltype of a bare name yields its declared type

20 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs