1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <type_traits>
#include <iostream>
template <typename T, typename...>
struct all_same_type;
template <typename T>
struct all_same_type<T>
{
static constexpr bool value = true;
};
template <typename T, typename U, typename... Us>
struct all_same_type<T, U, Us...>
{
static constexpr bool value =
std::is_same<T, U>::value && all_same_type<T, Us...>::value;
};
template <typename T> constexpr T add14(T const& x) { return x; }
template <typename T, typename... Ts,
typename = std::enable_if_t<all_same_type<T, Ts...>::value>>
constexpr T add14(T const& x, Ts const&... xs) { return x + add14(xs...); }
int main()
{
std::cout << add14(1, 2, 3, 4, 5) << '\n';
}
|