bayesNie
bayesNie

Reputation: 59

Why explicitly specify the type of a template argument?

A function template has the prototype as such:

template <int pot>
inline int gridMaxSelection(Eigen::Vector3f* grads, bool* map_out, 
                            int w, int h, float THFac); 

And it's called like this:

  if (sparsityFactor == 1)
    numGoodPoints = gridMaxSelection<1>(grads, map, w, h, THFac);
  else if (sparsityFactor == 2)
    numGoodPoints = gridMaxSelection<2>(grads, map, w, h, THFac);
  else if (sparsityFactor == 3)
    numGoodPoints = gridMaxSelection<3>(grads, map, w, h, THFac);
  else if (sparsityFactor == 4)
    numGoodPoints = gridMaxSelection<4>(grads, map, w, h, THFac); 

Why don't simply make the pot argument be function argument? Any efficiency concerns?

Upvotes: 0

Views: 34

Answers (1)

cigien
cigien

Reputation: 60208

No, there probably won't be any efficiency concerns either way. There usually aren't.

There are differences between template non-type parameters and function parameters, in terms of what you can do with them. e.g. if your function definition needs that parameter to be used in an integral constant expression, you have to make it a template parameter:

template<int n> 
void f() 
{
  static_assert(n == 42);  // ok
}

Because a function parameter cannot be used inside an expression that needs to be an integral constant expression:

void f(int const n) 
{
  static_assert(n == 42);  // error
}

Upvotes: 2

Related Questions