msvc 编译以下代码(使用/permissive- 编译器开关)、clang 和 gcc do not :
template<auto val>
struct S{
static constexpr auto val = val;
};
int main() {
return S<4>::val;
}
我认为这只是一个 msvc 错误,但我很好奇这里的标准是否含糊不清。
最佳答案
标准对此是明确的,模板参数不能以任何理由重新声明,参见 [temp.local]/6 :
A template-parameter shall not be redeclared within its scope (including nested scopes). A template-parameter shall not have the same name as the template name.
[ Example:
template<class T, int i> class Y { int T; // error: template-parameter redeclared void f() { char T; // error: template-parameter redeclared } }; template<class X> class X; // error: template-parameter redeclared
— end example ]
所以 MSVC 行为(给定
/permissive-
标志)看起来像是一个错误。
关于c++ - C++ struct 静态成员变量可以隐藏非类型模板参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68712964/