[英]Partial template specialization, using a templatized type (nested template classes)
给定通用函数foo:
template<typename T> void foo(T a) { a(); }
我想专门针对Bar类型使用此函数,但是Bar本身具有多个模板参数。 我试图按如下方式专门化foo():
template<typename... Args> void foo<Bar<Args...> >(Bar<Args...> a) { a(42); }
但是,这不太起作用。 有人可以帮我吗? 谢谢
没有功能模板的部分专业化之类的东西。 一种解决方法是委托一个类模板,该模板实际上可以是部分专用的。 遵循以下原则:
template <typename T>
struct FooImpl {
static void foo(T a) { /* general implementation */ }
};
template<typename... Args>
struct FooImpl<Bar<Args...>> {
static void foo(Bar<Args...> a) { /* special implementation */ }
};
template<typename T>
void foo(T a) { FooImpl<T>::foo(a); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.