Can the ternary (conditional) operator be used as an analogous to constexpr if()
, introduced in C++17?
I would like to add some conditionality to member variables initialization in a template. Would the following expression resolve at compile time or runtime? If so, is there any other operator that resolves at compile time such that template specialisation can be avoided?
template<int a>
struct hello {
constexpr static int n = (a != 0) ? 10 : 20;
}
constexpr
instead ofconst
, especially if compile-time evaluation is important to you (although in this case it will most likely not make a difference.) – Compensation#if
. – Letishaletitia