“If constexpr” in C++17 does not work in a non-templated function

I would like to know why “if constexpr” works only in template functions, even if the type is deduced by the decltype from the input parameter.

This is by design.

if constexpr will not instantiate the branch not taken if it’s within a template. It won’t just treat the branch not taken as token soup and avoid parsing it or performing semantic analysis entirely. Both sides are still going to be analyzed, and since *value is ill-formed for ints, that’s an error.

You simply can’t use if constexpr to avoid compiling non-template code. It’s only to avoid instantiating template code that’s potentially invalid-for-the-particular-specialization.

Leave a Comment