0

I would like to create a template, where i can use different vector types, and a constant as the same type of the vector. I would like to able to pass the constant as template parameter in order to let the compiler optimize it, without explicit specialization.

So i tried to re-use the type parameter, but i got illegal type.

#include <vector>
using namespace std;

template<typename WEIGHT, WEIGHT multiplier>
void test_multipier(vector<WEIGHT> &v)
{
    uint16_t num = 16;
    /*...*/
    WEIGHT w = multiplier* (num);
    v.push_back(w);
}

int main()
{
    vector<double> test_vector;

    test_multipier<double,0.01>(test_vector); 
    //^^ Error C2993 'double': illegal type for non-type template parameter 'multiplier'


    return 0;
}
becike
  • 195
  • 1
  • 8

1 Answers1

2

Copied from cppreference.com :

[A non-type template parameter's type] is one of the following types (optionally cv-qualified, the qualifiers are ignored):

  • std::nullptr_t (since C++11);
  • integral type;
  • lvalue reference type (to object or to function);
  • pointer type (to object or to function);
  • pointer to member type (to member object or to member function);
  • enumeration type.

double cannot be used as a non-type template parameter.

Quentin
  • 62,093
  • 7
  • 131
  • 191