Created
January 16, 2019 15:59
-
-
Save enfiskutensykkel/be669814b3fdc382849d2051f078b49a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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