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

前端 未结 6 899
刺人心
刺人心 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:52

    There are lots of reasons, like doing template metaprogramming (check Boost.MPL). But there is no need to go that far, C++11's std::tuple has an accessor std::get that needs to be indexed at compile time, since the result is dependent on the index.

提交回复
热议问题