1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Inside a function template, what happens to the branch of…

Added 30 Nov -0001

Inside a function template, what happens to the branch of an if constexpr whose condition is false for a given instantiation?

  1. It is removed before parsing, so even syntax errors in it are ignored
  2. It is compiled and executed at run time only if its condition is true
  3. It is discarded and not instantiated, so dependent code there may be invalid Correct answer
  4. It is still instantiated and type-checked, then removed later as a dead branch

Correct answer

C. It is discarded and not instantiated, so dependent code there may be invalid

17 views

MCQSEXAM

Explanation

if constexpr discards the untaken branch during instantiation, so dependent code inside it is not instantiated and may be ill-formed for that particular type. The branch must still be syntactically valid and its condition must be a constant expression. This is what lets one function template serve types with and without a given member, replacing tag dispatch or overload-based SFINAE.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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