Is there any special reason why if constexpr
can't be used outside function's body (why it is not implemented in C++)? For example like in code below that uses if constexpr
to decide what specializations of sub-class to make:
template <int Size>
struct A {
template <typename T> struct B;
if constexpr(Size >= 32) {
template <> struct B<uint32_t> {};
}
if constexpr(Size == 32) {
using MaxT = uint32_t;
}
if constexpr(Size >= 64) {
template <> struct B<uint64_t> {};
}
if constexpr(Size == 64) {
using MaxT = uint64_t;
}
};
That syntax above is much more handy than using std::enable_if and std::conditional_t and other tricks.
Syntax above allows to generate any code based on value of compile time constant. And would be a great replacement for #ifdef
/#endif
macros in many cases.
As commented by @StoryTeller-UnslanderMonica, there exists similar concept in other languages like static if in D.
Does anyone know if there were any proposals regarding this syntax and any future plans? If they were rejected then why?