模板参数中"T"和"const T"之间有什么区别吗?

iam*_*ind 31 c++ syntax templates const

以下2种语法之间有什么区别:

template<int N> struct A;         // (1)
Run Code Online (Sandbox Code Playgroud)

template<const int N> struct A;   // (2)
Run Code Online (Sandbox Code Playgroud)

何时使用每种语法的一般准则?

Xeo*_*Xeo 28

没有.

§14.1 [temp.param] p5

[...] 模板参数的顶级cv限定符在确定其类型时会被忽略.

  • @JN:不,这句话特别指的是非类型模板参数(你不能有'const typename`反正:P). (2认同)

Pub*_*bby 5

我发现这是对标准的快速搜索:

template<const short cs> class B { };
template<short s> void g(B<s>);
void k2() {
    B<1> b;
    g(b); // OK: cv-quali?ers are ignored on template parameter types
}
Run Code Online (Sandbox Code Playgroud)

评论说他们被忽略了.

我建议不要const在模板参数中使用,因为它是不必要的.请注意,它也不是"暗示" - 它们是不同的常量表达式const.