C ++模板部分特化错误

时间:2011-01-05 10:51:32

标签: c++ templates g++ template-specialization partial-specialization

以下代码给出了编译错误: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)));
}

什么错了?我正在尝试将grid_residue专门用于课程Q64

UPDATE:

更改了语法。现在收到错误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)));
}

感谢

3 个答案:

答案 0 :(得分:9)

功能不能部分专业化!使用函数重载:template <int GRIDD> inline Q64 grid_residue(Q64 amount)或将函数包装在一个类型中(可以部分专门化)。

答案 1 :(得分:1)

你不能部分专门化功能。

答案 2 :(得分: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);
}

编译/链接确定(VS2010)。