使用 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;

Demo.