5

以下代码给了我一个编译错误: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

更新:

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

谢谢

4

3 回答 3

9

函数不能部分特化!要么使用函数重载:template <int GRIDD> inline Q64 grid_residue(Q64 amount)要么将你的函数包装在一个类型中(可以部分特化)。

于 2011-01-05T10:54:45.163 回答
1

您不能部分专门化函数。

于 2011-01-05T11:03:51.430 回答
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)。

于 2011-01-05T11:48:47.517 回答