Do I need to put constexpr after else-if?

Do we need to put constexpr after every if statement in if-else block in these kind of situations?

Yes. The else-if block1 is a lie :), there are only if blocks1 and else blocks1. This is how your code is seen by the compiler:

if constexpr (std::is_same_v<int, T>)
    return {a, 0.0};
else // {
    if (std::is_same_v<double, T>)
        return {0, a};
        return {0, 0.0};
// }

else if (/*...*/) is just a formatting convention that everyone uses. As such, you can clearly see that the second constexpr is needed.

1: “block” is not the correct terminology. if is a statement (with optional else part). A block is { /*...*/ }.

Leave a Comment