用using专门化可变模板
Specializing variadic template with using
晚上好
我在特殊化可变模板时遇到麻烦。我需要以下模板:
template<typename... Ts>
using OPT = decltype(operator+(std::declval<Ts>()...))(Ts...);
问题是,当我尝试使用
时,这不会编译OTP<double,double>
所以我尝试通过
专门化它template<>
using OPT<double,double> = double;
但是现在我得到了错误
error: expected unqualified-id before ‘using’
using OPT<double,double> = double;
有没有人知道这个方法或者我做错了什么?
感谢您的阅读和帮助!
需要一个结构体来实现这一点,因为别名模板不能专门化,也不能引用自己。
#include <utility>
template<typename T, typename... Ts>
struct sum_type {
using type = decltype(std::declval<T>() + std::declval<typename sum_type<Ts...>::type>());
};
template <typename T>
struct sum_type<T> {
using type = T;
};
template<typename... Ts>
using OPT = typename sum_type<Ts...>::type;
演示。
相关文章:
- 是否可以对零模板参数进行模板专门化
- "using namespace std;"在C++的作用是什么?
- 尝试根据类中 typedef 的存在来专门化模板函数
- vector<vector<double>> to mxArray using memcpy
- Using C++ CryptAPI:CNG Create CSR
- STLPort using C++11
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- Using getchar_unlocked()
- 类作用域的类型别名"using":[何时]方法中的用法可以先于类型别名?
- 如何基于模板化类的基类专门化成员函数
- 嵌套命名空间的"using"指令,但需要命名内部命名空间
- 为什么"using System;"不被视为不良做法?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- 'using namespace'实现细节的便捷方法(仅标头库)?
- 如何为指向复杂值的迭代器专门化算法?
- 专门化模板覆盖函数/避免对象切片
- 我能否根据其运算符()的签名专门化可变参数模板参数
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- C++11`using`关键字:专门化模板参数的模板别名
- 用using专门化可变模板