用using专门化可变模板

Specializing variadic template with using

本文关键字:using 专门化      更新时间:2023-10-16

晚上好

我在特殊化可变模板时遇到麻烦。我需要以下模板:

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;

演示。