非类型模板参数不引用任何声明?

Dra*_*neg 2 c++ templates

#include <iostream>                                                                                                               
#include <string>                                                                                                                 

using namespace std;                                                                                                              

template <typename T,                                                                                                             
          typename T::type N,                                                                                                     
          typename T::strata& X>                                                                                                  
struct SomeClass{};                                                                                                               

struct S1                                                                                                                         
{                                                                                                                                 
  typedef int type;                                                                                                               
  typedef string strata;                                                                                                          
};                                                                                                                                

int main () {                                                                                                                     
  SomeClass<S1, 3, string("erg")> x;                                                                                              
}    
Run Code Online (Sandbox Code Playgroud)

失败并显示消息:

 g++ templ.cc -o templ -std=c++14                                                                                   
 templ.cc:18:20: error: non-type template argument does not refer to any declaration                                               
   SomeClass<S1, 3, string("erg")> x;                                                                                              
                    ^~~~~~~~~~~~~                                                                                                  
 templ.cc:8:24: note: template parameter is declared here                                                                          
           typename T::strata& X> 
Run Code Online (Sandbox Code Playgroud)

为什么它适用于 int 而不适用于字符串?为什么它说字符串是非类型参数?

asc*_*ler 5

作为值而不是类型或模板的模板参数简称为“非类型模板参数”。

由于引用而失败。你会得到一个类似的错误的3,如果你有typename T::type& N

引用cppreference.com

实例化具有非类型模板参数的模板时,以下限制适用:

  • 对于左值引用参数,实例化时提供的参数不能是临时的、未命名的左值或没有链接的命名左值(换句话说,参数必须有链接)。

所以你的临时是无效的。但你可以这样做:

std::string erg("erg");
int main () {                                                                                                                     
  SomeClass<S1, 3, erg> x;                                                                                              
}
Run Code Online (Sandbox Code Playgroud)

  • 因为具有自动存储持续时间的变量没有链接。 (4认同)