Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <type_traits>
- #include <tuple>
- // This implementation of FDBB_RETURNS is c++11 compliant, but does not compile
- // (compiler complains that template argument deduction failed)
- // #define FDBB_RETURNS(expr) -> decltype(expr) { return expr; }
- // This works, but is c++14 and above only:
- #define FDBB_RETURNS(expr) { return expr; }
- namespace detail {
- template <int I, class... T>
- auto tuple_sum(const std::tuple<T...>& t, std::false_type)
- FDBB_RETURNS(std::get<I>(t))
- template <int I, class... T>
- auto tuple_sum(const std::tuple<T...>& t, std::true_type)
- FDBB_RETURNS(std::get<I>(t) + tuple_sum<(I+1)>(t, std::integral_constant<bool, (I+2<sizeof...(T))>()))
- } // namespace detail
- template<class... T>
- auto tuple_sum(const std::tuple<T...>& t)
- FDBB_RETURNS(detail::tuple_sum<0>(t, std::integral_constant<bool, (1<sizeof...(T))>()))
- int main() {
- std::cout << tuple_sum(std::make_tuple(1,2,3)) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement