if constexpr in C++17, I was wondering if we could also extend the same behavior to switch-statements. I think it would fit well in the language and simplify complicated compile-time branching.
It would also enable compile-time usage of the C++ Core Guideline ES.70 (Prefer a switch-statement to an if-statement when there is a choice)
For example:
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
int do_integer() {
// if constexpr
if constexpr (sizeof(T) == 1) {
return do_i8();
} else if constexpr (sizeof(T) == 2) {
return do_i16();
} else if constexpr (sizeof(T) == 4) {
return do_i32();
} else if constexpr (sizeof(T) == 8) {
return do_i64();
} else {
// We *shouldn't* get here
}
// Simplified with switch constexpr
switch constexpr (sizeof(T)) {
case 1: return do_i8();
case 2: return do_i16();
case 4: return do_i32();
case 8: return do_i64();
default: // Is there any architecture with other-sized integers?
}
}
Even in that relatively simple example, the switch-method, in my opinion, is clearer and more expressive.
Surprisingly, I couldn't find any discussion in this regard. Should there be any, please point me to it.
- Elias
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
int do_integer() {
// if constexpr
if constexpr (sizeof(T) == 1) {
return do_i8();
} else if constexpr (sizeof(T) == 2) {
return do_i16();
} else if constexpr (sizeof(T) == 4) {
return do_i32();
} else if constexpr (sizeof(T) == 8) {
return do_i64();
} else {
// We *shouldn't* get here
}
// Simplified with switch constexpr
switch constexpr (sizeof(T)) {
case 1: return do_i8();
case 2: return do_i16();
case 4: return do_i32();
case 8: return do_i64();
default: // Is there any architecture with other-sized integers?
}
}
Even in that relatively simple example, the switch-method, in my opinion, is clearer and more expressive.
Surprisingly, I couldn't find any discussion in this regard. Should there be any, please point me to it.
For consistency and flexibility's sake, perhaps constexpr versions of for, while, and do-while statements should also be added at the same time?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/25e84110-5b8b-471d-a72b-2ed5b63f4865%40isocpp.org.
constexpr void loop(int i) {while (i++);}
// GCC, VS & ICC currently return true if loop provably finite, false otherwise
// Clang always returns false
noexcept(loop(foo))