C++模板部分特化错误

发布于 2024-10-10 03:31:38 字数 745 浏览 6 评论 0原文

下面的代码给了我一个编译错误:class Q64 is not a valid type for a templateconstant argument

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类。

更新:

更改了语法。现在出现错误错误:函数模板部分特化'grid_residue'不允许

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九八野马 2024-10-17 03:31:38

函数不能部分特化!要么使用函数重载:template;内联 Q64 grid_residue(Q64 amount) 或将函数包装在类型中(可以部分专门化)。

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).

雪若未夕 2024-10-17 03:31:38

您不能部分特化函数。

You cannot partially specialise functions.

带上头具痛哭 2024-10-17 03:31:38
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)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文