为什么我们使用非类型模板参数? [英] Why we use non-type template arguments?
本文介绍了为什么我们使用非类型模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我明白这个概念,但我不知道为什么我需要使用非类型的模板参数?
I understand the concept but i don't know why i require to use non-type template arguments ?
推荐答案
很多用例,所以让我们来看看它们是不可或缺的一些情况:
There are many use-cases, so let's look at a couple of situations where they are indispensable:
-
固定大小的数组或 matrix 类,请参阅示例C ++ 11 std :: array 或 boost :: array
Fixed sized array or matrix classes, see for example C++11 std::array or boost::array.
std :: begin ,或任何需要固定大小C样式数组大小的代码,例如:
A possible implementation of std::begin for arrays, or any code that needs the size of a fixed size C style array, for example:
返回数组的大小:
template <typename T, unsigned int N>
unsigned int size(T const (&)[N])
{
return N;
}
它们在模板元编程中也非常有用。
They are also extremely useful in template metaprogramming.
这篇关于为什么我们使用非类型模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文