const模板参数 [英] const in template argument
问题描述
const
关键字在此模板中的作用是什么?
template< class T,int const ROWNUM,int const COLNUM>
class Matrix
这是否意味着此模板只接受 const
作为参数?如果是,是否有一种方法可以传递一个变量作为 COLNUM
和 ROWNUM
?
(当我尝试传递一个变量作为模板的COLNUM时,它会给出一个错误:IntelliSense:expression必须有一个常量值)
它被忽略:
:14.1 / 4]:
非类型的模板参数应具有以下类型之一(可选cv限定 p>
- 积分或枚举类型,
- 指向对象或函数指针的指针,
- 指向对象或引用函数的左值的引用,
- 指向成员的指针,
std :: nullptr_t
。
[C ++ 11:14.1 / 5] / code> [注意:其他类型在下面明确禁止,或者隐含地由管理 template-argument (14.3)格式的规则禁止。 -end note ] 在确定其类型时会忽略模板参数 。
在C ++ 03中的同一位置上存在相同的字词。
这部分是因为模板参数必须在编译时知道。因此,无论您是否有 const
,您可能不会传递一些变量值:
template< int N>
void f()
{
N = 42;
}
template< int const N>
void g()
{
N = 42;
}
int main()
{
f< 0>
g< 0>();
static const int h = 1;
f< h>();
g< h>();
}
prog.cpp: > void f()[with int N = 0] ':
prog.cpp:15:从这里实例化
prog.cpp:4:error :
prog.cpp:在函数' void g()[with int N = 0] 中:
prog.cpp:16:从此处实例化
prog.cpp:10:错误:需要作为左操作数分配的左值 >
prog.cpp:in function' void f()[with int N = 1] ':
prog.cpp:19:instantiated from here
prog.cpp:4:错误:需要作为左操作数赋值的值
prog.cpp:在函数' void g )[with int N = 1] ':
prog.cpp:20:从这里实例化
prog.cpp:10:error:需要作为左操作数分配的左值
What is the effect of the const
keyword in this template?
template <class T, int const ROWNUM, int const COLNUM>
class Matrix
Does it mean that this template only accept a const
as parameter? If so, is there a way to pass a variable as the COLNUM
and ROWNUM
?
(when I try to pass a variable as the COLNUM for the template, it gives an error: "IntelliSense: expression must have a constant value")
It's ignored:
[C++11: 14.1/4]:
A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
- integral or enumeration type,
- pointer to object or pointer to function,
- lvalue reference to object or lvalue reference to function,
- pointer to member,
std::nullptr_t
.
[C++11: 14.1/5]:
[ Note: Other types are disallowed either explicitly below or implicitly by the rules governing the form of template-arguments (14.3). —end note ] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.
The same wording is present at the same location in C++03.
This is partially because template arguments must be known at compile-time anyway. So, whether you have the const
there or not, you may not pass some variable value:
template <int N>
void f()
{
N = 42;
}
template <int const N>
void g()
{
N = 42;
}
int main()
{
f<0>();
g<0>();
static const int h = 1;
f<h>();
g<h>();
}
prog.cpp: In function ‘void f() [with int N = 0]’:
prog.cpp:15: instantiated from here
prog.cpp:4: error: lvalue required as left operand of assignment
prog.cpp: In function ‘void g() [with int N = 0]’:
prog.cpp:16: instantiated from here
prog.cpp:10: error: lvalue required as left operand of assignment
prog.cpp: In function ‘void f() [with int N = 1]’:
prog.cpp:19: instantiated from here
prog.cpp:4: error: lvalue required as left operand of assignment
prog.cpp: In function ‘void g() [with int N = 1]’:
prog.cpp:20: instantiated from here
prog.cpp:10: error: lvalue required as left operand of assignment
这篇关于const模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!