专门处理一个参数(C++模板)的两个模板参数
Specializing with two template arguments for an argument (C++ template)
我不知道如何正确描述这个问题,但基本上我想知道的是,这样的东西可以毫无问题地编译:
// prototype
template <class T>
void pretty_function(T arg);
// specialization
template <class U, class V>
void pretty_function<U<V>>(T arg);
所以我想用类型U<V>,其中类型U需要模板参数V。我想我可以在本地工作站上轻松地测试它,但我只是把它留在这里供将来参考。
听起来您想要声明pretty_function
的专门化,它只接受形式为U<V>
的类型,其中U
可以是任何类模板,V
可以是任何类型。这将是部分专门化,因为没有完全指定模板参数T
。C++不支持函数模板的部分专用化。通常的解决方法是将其发送到一个可以部分专用的助手类模板:
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++如果两个模板函数都与参数列表匹配,将调用哪个模板
- 如何从两个不同的函数传递参数
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 命令行参数,cant 或两个变量
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 如何创建两个具有相同名称和不同返回类型并基于布尔参数运行的函数
- 具有两个或多个模板参数的 C++ assigment 运算符
- 检查两个模板参数是否相同
- sscanf() 有两个字符串参数
- 如何在两个树通用的函数中创建一个参数?
- 为什么C++不允许两个同名的函数/类模板,区别仅在于非类型模板参数(整型)的类型?
- 为什么模板参数推导不适用于仅指定前两个参数的可变参数模板类?
- 用相同的参数声明两个构造函数的最偶像化的方法是什么?
- C++如何在 switch 语句中放置两个参数
- 为什么我不能用两个参数重载 C++ 运算符 []?
- 专门处理一个参数(C++模板)的两个模板参数
- C++stoi:这两个重载都无法转换所有参数类型
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)