C++ 模板参数中的常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13435235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
const in template argument
提问by Chin
What is the effect of the constkeyword in this template?
const这个模板中关键字的作用是什么?
template <class T, int const ROWNUM, int const COLNUM>
class Matrix
Does it mean that this template only accept a constas parameter? If so, is there a way to pass a variable as the COLNUMand ROWNUM?
这是否意味着这个模板只接受 aconst作为参数?如果是这样,有没有办法将变量作为COLNUMand传递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")
(当我尝试将变量作为模板的 COLNUM 传递时,它给出了一个错误:“智能感知:表达式必须有一个常量值”)
回答by Lightness Races in Orbit
It's ignored:
它被忽略:
[C++11: 14.1/4]:A non-type template-parametershall 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-qualifierson the template-parameterare ignoredwhen determining its type.
[C++11: 14.1/4]:非类型模板参数应具有以下类型之一(可选cv 限定):
- 整数或枚举类型,
- 指向对象的指针或指向函数的指针,
- 对对象的左值引用或对函数的左值引用,
- 指向成员的指针,
std::nullptr_t.
[C++11: 14.1/5]:[注意:其他类型在控制模板参数形式的规则(14.3)之下或隐含地被禁止。—end note]在确定模板参数的类型时,会忽略模板参数上的顶级cv 限定符。
The same wording is present at the same location in C++03.
在 C++03 中的相同位置存在相同的措辞。
This is partially because template arguments must be known at compile-time anyway. So, whether you have the constthere or not, you may notpass some variable value:
这部分是因为模板参数无论如何都必须在编译时知道。所以,不管你有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: 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
prog.cpp:在函数“ void f() [int N = 0]”中:
prog.cpp:15:从这里实例化
prog.cpp:4:错误:左值需要作为赋值
prog.cpp 的左操作数:在函数中' void g() [int N = 0]':
prog.cpp:16: 从这里实例化
prog.cpp:10: 错误:左值需要作为赋值
prog.cpp 的左操作数: 在函数中 ' void f() [ with int N = 1]':
prog.cpp:19: 从这里实例化
prog.cpp:4: 错误:左值需要作为赋值
prog.cpp 的左操作数:在函数 ' void g() [with int N = 1]':
prog.cpp:20: 从这里实例化
prog.cpp:10: 错误:左值需要作为赋值的左操作数
回答by olibre
constis not required in your case
const在您的情况下不需要
for instance, both classes Matrix_Aand Matrix_Bbelow are the same for the compiler point of view. consthere is just to enforce the fact that ROWNUMand COLNUMare constant for humans point of view, but not required.
例如,从编译器的角度来看,这两个类Matrix_A和Matrix_B下面的类是相同的。这里只是为了强制执行这样一个事实,对于人类的观点来说和是不变的,但不是必需的。constROWNUMCOLNUM
template <class T, int const ROWNUM, int const COLNUM>
class Matrix_A
{
};
template <class T, int ROWNUM, int COLNUM>
class Matrix_B
{
};
Moreover following class Matrix_Calso specify similar constant variables ROWNUMand COLNUMin another way:
而且下面的类Matrix_C也指定类似常数变量ROWNUM,并COLNUM以另一种方式:
template <class T>
class Matrix_C
{
static int const ROWNUM = 5;
static int const COLNUM = 20;
};
// the following three objects use constant variables ROWNUM and COLNUM
Matrix_A<bool,5,20> a;
Matrix_B<bool,5,20> b;
Matrix_C<bool> c;

