下面的代码打印val2
在两个f()
调用上。f()
根据枚举值执行特定分支的正确方法是什么?
enum class E
{
val1,
val2
};
using val1_t = std::integral_constant<E, E::val1>;
using val2_t = std::integral_constant<E, E::val2>;
template <typename T>
void f(T t)
{
if constexpr (std::is_same_v<T, val1_t>)
{
std::cerr << "val1\n";
}
else
{
std::cerr << "val2\n";
}
}
int main()
{
f(E::val1);
f(E::val2);
}