Reason for using non-type template parameter instead of regular parameter?

前端 未结 6 900
刺人心
刺人心 2020-12-03 00:54

In C++ you can create templates using a non-type template parameter like this:

template< int I >
void add( int& value )
{
  value += I;
}

int main         


        
6条回答
  •  被撕碎了的回忆
    2020-12-03 01:40

    There are many applications for non-type template arguments; here are a few:

    You can use non-type arguments to implement generic types representing fixed-sized arrays or matrices. For example, you might parameterize a Matrix type over its dimensions, so you could make a Matrix<4, 3> or a Matrix<2, 2>. If you then define overloaded operators for these types correctly, you can prevent accidental errors from adding or multiplying matrices of incorrect dimensions, and can make functions that explicitly communicate the expected dimensions of the matrices they accept. This prevents a huge class of runtime errors from occur by detecting the violations at compile-time.

    You can use non-type arguments to implement compile-time function evaluation through template metaprogramming. For example, here's a simple template that computes factorial at compile-time:

    template  struct Factorial {
        enum { 
           result = n * Factorial::result
        };
    };
    template <> struct Factorial<0> {
        enum {
           result = 1
        };
    };
    

    This allows you to write code like Factorial<10>::result to obtain, at compile-time, the value of 10!. This can prevent extra code execution at runtime.

    Additionally, you can use non-type arguments to implement compile-time dimensional analysis, which allows you to define types for kilograms, meters, seconds, etc. such that the compiler can ensure that you don't accidentally use kilograms where you meant meters, etc.

    Hope this helps!

提交回复
热议问题