c++ - C++ struct 静态成员变量可以隐藏非类型模板参数吗?

标签 c++ language-lawyer c++20 non-type-template-parameter

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/

相关文章:

c++ - 有状态的元编程格式不正确吗(还)?

c++ - 是否可以确保在编译时最多调用一次 constexpr 函数?

c++ - 将可变大小数组传递给 DLL 的选项有哪些?

c++ - 为什么使用 C++ 接口(interface)作为类之间的连接?

c++ - boost:spirit::qi 和制表符作为分隔符

c++ - XERCESC 2.7 内存泄漏问题

c++ - 允许 `this->` 访问依赖基类的成员的规则是什么?

c++ - 不完整类型适用于 gcc,但不适用于 clang 和 msvc

c++ - 在哪个访问控制上下文中评估概念?

c++ - 是否可以在 C++20 的 requires 子句中初始化模板内部类?