Inside a function template, what happens to the branch of an if constexpr whose condition is false for a given instantiation?
Correct answer
C. It is discarded and not instantiated, so dependent code there may be invalid
17 views
C. It is discarded and not instantiated, so dependent code there may be invalid
17 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.