Skip to content

Instantly share code, notes, and snippets.

@enfiskutensykkel
Created January 16, 2019 15:59
Show Gist options
  • Save enfiskutensykkel/be669814b3fdc382849d2051f078b49a to your computer and use it in GitHub Desktop.
Save enfiskutensykkel/be669814b3fdc382849d2051f078b49a to your computer and use it in GitHub Desktop.
#include <type_traits>
template <typename T, typename = std::enable_if_t<std::is_compound_v<T>>>
constexpr T sum(const T& v) noexcept
{
return v;
}
template <typename T, typename = std::enable_if_t<!std::is_compound_v<T>>>
constexpr T sum(T v) noexcept
{
return v;
}
template <typename T, typename... Args, typename = std::enable_if_t<std::is_compound_v<T>>>
constexpr T sum(const T& first, Args... args) noexcept
{
return sum(first, sum(args...));
}
template <typename T, typename... Args, typename = std::enable_if_t<!std::is_compound_v<T>>>
constexpr T sum(T first, Args... args) noexcept
{
return sum(first, sum(args...));
}
struct tuple
{
int first;
int second;
};
constexpr tuple sum(const tuple& a, const tuple& b)
{
return tuple{a.first + b.first, a.second + b.second};
}
int main()
{
auto n = sum(tuple{1, 1}, tuple{2, 2}, tuple{3, 3});
return n.first;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment