我编写了以下 C++17 代码:
constexpr bool gDebug = true;
template <typename T> constexpr const T& Select(const bool pCondition, const T& a, const T& b)
{
if constexpr (pCondition)
{
return a;
}
else
{
return b;
}
}
然后我这样称呼它:
int c = Select<QString>(gDebug, a, b); // In .cpp
我error: ‘pCondition’ is not a constant expression
接if constexpr
电话。
为什么?这不应该工作吗?