我不知道如何正确描述这个问题,但基本上我想知道的是这样的东西可以毫无问题地编译:
// prototype
template <class T>
void pretty_function(T arg);
// specialization
template <class U, class V>
void pretty_function<U<V>>(T arg);
所以我想用 U< V > 类型专门化 T 类型,其中 U 类型需要一个模板参数 V。我想我可以在我的本地工作站上轻松测试它,但我只是将它留在这里以供将来引用。
最佳答案
听起来您想声明 pretty_function
的特化只接受 U<V>
形式的类型其中 U
可以是任何类模板和V
可以是任何类型。这将是一个部分特化,因为模板参数 T
没有完全指定。 C++ 不支持函数模板的部分特化。通常的解决方法是分派(dispatch)到可以部分特化的帮助类模板:
namespace detail {
template <class T>
struct pretty_function_helper {
static void doit(T arg) { /* implementation */ }
};
// partial specialization
template <template <class> class U, class V>
struct pretty_function_helper<U<V>> {
static void doit(U<V> arg) { /* implementation */ }
};
}
template <class T> void pretty_function(T arg) {
detail::pretty_function_helper<T>::doit(arg);
}
关于c++ - 专攻一个参数的两个模板参数(C++ 模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51665893/