使用模板化类型(嵌套模板类)的部分模板专用化
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); }
相关文章:
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 为什么依赖模板类型在部分专用化中不可推导?
- 具有可变参数非类型参数的模板专用化
- 检查子类型时的专用方法模板
- 特定类型的模板函数的专用化
- 将"模板<类型名 T>zero()"扩展/专用为可调用的"T"
- 模板专用化和明确指定返回类型与自动
- 将返回类型专用化为 void 或 const 左值引用
- 专用于类型集的函数模板
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- 为什么 std::optional::value_or 没有默认 ctor 类型的专用化?
- 具有模板专用化的泛型类型转换
- cpp 模板专用化,错误说参数 1 的类型为 T,这取决于参数 T
- 具有不同非类型模板参数的模板类部分专用化
- 从具有部分专用化的boost:hana::set中提取类型失败
- 带有void类型和参数的C++11模板专用化
- 常量和非常量类型的相同模板专用化
- 具有专用和非专用类型的模板重载
- 是否可以派生自和"respecialize"具有派生自专用类型的类型的专用类?