5

The following code is giving me a compilation error: class Q64 is not a valid type for a template constant parameter

template<int GRIDD, class T>
INLINE T grid_residue(T amount) {
  T rem = amount%(GRIDD);
  if (rem > GRIDD/2) rem -= GRIDD;
  return rem;
}


template<int GRIDD, Q64>
INLINE Q64 grid_residue(Q64 amount) {
  return Q64(grid_residue<GRIDD, int64_t>(to_int(amount)));
}

Whats wrong? I am trying to specialize grid_residue for class Q64.

UPDATE:

Changed syntax. Now getting error error: function template partial specialization 'grid_residue<GRIDD, Q64>' is not allowed

template<int GRIDD>
INLINE Q64 grid_residue(Q64 amount) {
    return Q64(grid_residue<GRIDD, int>(to_int(amount)));
}

thanks

3 Answers3

9

Functions cannot be partially specialized! Either use function overloading: template <int GRIDD> inline Q64 grid_residue(Q64 amount) or wrap your function in a type (which can be partially specialized).

ltjax
  • 15,837
  • 3
  • 39
  • 62
  • function overloading... isn't that kind of specialization? however, I still get errors with the modified code (i'll post). –  Jan 05 '11 at 11:05
  • found the syntax on http://bytes.com/topic/c/answers/61482-partial-specialization-function-template –  Jan 06 '11 at 10:56
1

You cannot partially specialise functions.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0
struct test
{

};

template<int GRIDD, class T>
T grid_residue(T amount) 
{
    std::cout << "template<int GRIDD, class T> T grid_residue(T amount)" << " GRIDD: " << GRIDD << std::endl;
    return T();
}


template<int GRIDD>
test grid_residue(test amount) 
{
    std::cout << "template<int GRIDD> test grid_residue(test amount)" << " GRIDD: " << GRIDD << std::endl;
    int inp = 0;
    grid_residue<GRIDD,int>(inp);
    return test();
}


int 
_tmain(int argc, _TCHAR* argv[])
{
    test amount;
    grid_residue<19>(amount);
    std::string inValue;
    grid_residue<19>(inValue);
}

Compiles/links ok (VS2010).

ds27680
  • 1,993
  • 10
  • 11