I want to extract the bool value from template parameters, and use that value in somewhere else in the code. (More specifically, I want a if-else in compile time).
template<bool enable_xx>
struct A {
void DoSomething() {
if (enable_xx) {
// do something
} else {
// do something else
}
}
}
I'm using C++11, but if such feature exists for higher version C++ pls also tell me, thanks!
C++17 provides a way to do this using if constexpr
:
template<bool enable_xx>
struct A {
void DoSomething() {
if constexpr(enable_xx) {
// do something
} else {
// do something else
}
}
};
if constexpr
only runs at compiletime, and it does exactly what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With