I understand the concept but i don't know why i require to use non-type template arguments ?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Another example of non type argument is:
Here the length of the data field is parameterised. Different instantiations of this struct can have different lengths of their arrays.
To program at compile-time. Consider the WikiPedia example,
There are a bunch of other examples on the WikiPedia page.
EDIT
As mentioned in the comments, the above example demonstrates what can be done rather than what people use in real projects.
A real-world example comes from combining non-type template arguments with template argument deduction to deduce the size of an array:
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.
A possible implementation of std::begin for arrays, or any code that needs the size of a fixed size C style array, for example:
return the size of an array:
They are also extremely useful in template metaprogramming.