[英]Template Parameters and template arguments
I am relatively new to C++(14) .我对 C++(14) 比较陌生。 And i am working on enhancing my current knowledge by reading through the official documentation from cpprefernce .我正在通过阅读cpprefernce的官方文档来增强我目前的知识。 I have some questions about template declaration : This page gives the following possible parameters(non-type template parameter) in the declarations for a class template .我有一些关于模板声明的问题: 此页面在类模板的声明中提供了以下可能的参数(非类型模板参数)。
type name(optional) // (1)
My question is :我的问题是:
1.)What is meant by type name
? 1.) type name
是什么意思? I am sure it is not same as typename
mentioned here .我确信它与这里提到的typename
。
Further on the same page , i see the example :在同一页面上,我看到了示例:
// simple non-type template parameter
template<int N>
struct S { int a[N]; };
However from //1
, i see the type name is optional , so i hypothesis the below should work but it does not:(Indeed i see the below declaration template<N>
can be seen as template argument with type param.) \\然而,从//1
,我看到类型名称是可选的,所以我假设下面应该工作但它没有:(实际上我看到下面的声明template<N>
可以被视为具有类型参数的模板参数。)\\
// simple non-type template parameter
template<N>
struct S { int a[N]; };
So where is my understanding/reading of official docs is going wrong ?那么我对官方文档的理解/阅读哪里出了问题?
Edit :编辑 :
I tried the following :我尝试了以下方法:
template< int>
struct S { int a[N]; };
which does not compile with the error 'N' was not declared in the scope
.不编译时出现错误'N' was not declared in the scope
。
For that to work, N
would need to be a type.为此, N
需要是一种类型。
Templates only accept a type followed by an optional name for that type.模板只接受一个类型,后跟该类型的可选名称。 So for instance this might work (though I haven't tried it):因此,例如这可能有效(尽管我还没有尝试过):
template <int> // ...
But note that N
is not declared.但请注意,未声明N
If you want to use N
in your template, you will need to declare N
like this:如果你想在你的模板中使用N
,你需要像这样声明N
:
template <int N> // ...
Here, N
is declared as an int
, so you can use it just as you would an int
.在这里, N
被声明为int
,所以你可以使用它就像你的int
。
Note: You also have the option of something like this:注意:您还可以选择以下内容:
template <class N> // ...
Here N
becomes a template replacement for a type.这里N
成为一个类型的模板替换。 It isn't an int as above.它不是上面的 int 。
Of course, that's probably not what you want.当然,这可能不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.