且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我们使用非类型模板参数?

更新时间:2023-11-30 22:33:22

很多用例,所以让我们来看看它们是不可或缺的一些情况:

There are many use-cases, so let's look at a couple of situations where they are indispensable:

  • 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.