此代码无法在MSVC上编译,错误代码为C2065“'test':未声明的标识符”。Clang编译时没有警告,GCC编译但通过-Wall
“警告:设置了变量'test',但未使用[-Wunused-but-set-variable]”来发出警告。可在Godbolt上进行比较。
template <typename T>
struct foo
{
constexpr auto operator()() const
{
if constexpr (constexpr auto test = true; test)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
foo<int> bar{};
bar();
return 0;
}
有多种方法可以进行此编译
foo
constexpr
test
在外部分配if constexpr
constexpr auto test = true;
if constexpr (test)
编译失败的原因是什么?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句