简体   繁体   English

如何使用类模板完全专门化功能模板?

[英]How do I fully specialize a function template with a class template?

template <typename T>
void foo(T t)
{
   ... // do stuff with type T
}

template <typename T>
class class_template
{
    // class body
};

template<>                       // failed attempt at full specialization
void foo(class_template<T> t)    // which doesn't work of course
{
    //full specialization for all classes of class_template
} 

In the above code how do I explicitly specialize function foo with a class template? 在上面的代码中,如何使用类模板显式地专门化foo函数?

In the above code how do I explicitly specialize function foo with a class template? 在上面的代码中,如何使用类模板显式地专门化foo函数?

You cannot. 你不能。 This is the whole point of partial specialisations. 这是部分专业的重点。 But they don't work for functions. 但是它们不适用于功能。

You have two solutions: 您有两种解决方案:

  • Overload the function. 重载功能。 This usually works. 这通常有效。
  • Refer the work to a class template, which can be partially specialised. 将工作参考类模板,该模板可以部分进行专门处理。 That is, inside your function, call a (static) function in a class template, and specialise that . 也就是说,你的函数中,调用类模板(静态)功能,并专注这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM