Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <tuple>
  4.  
  5. // This implementation of FDBB_RETURNS is c++11 compliant, but does not compile
  6. // (compiler complains that template argument deduction failed)
  7. // #define FDBB_RETURNS(expr) -> decltype(expr) { return expr; }
  8.  
  9. // This works, but is c++14 and above only:
  10. #define FDBB_RETURNS(expr) { return expr; }
  11.  
  12. namespace detail {
  13.  
  14. template <int I, class... T>
  15. auto tuple_sum(const std::tuple<T...>& t, std::false_type)
  16. FDBB_RETURNS(std::get<I>(t))
  17.  
  18. template <int I, class... T>
  19. auto tuple_sum(const std::tuple<T...>& t, std::true_type)
  20. FDBB_RETURNS(std::get<I>(t) + tuple_sum<(I+1)>(t, std::integral_constant<bool, (I+2<sizeof...(T))>()))
  21.  
  22. } // namespace detail
  23.  
  24. template<class... T>
  25. auto tuple_sum(const std::tuple<T...>& t)
  26. FDBB_RETURNS(detail::tuple_sum<0>(t, std::integral_constant<bool, (1<sizeof...(T))>()))
  27.  
  28. int main() {
  29. std::cout << tuple_sum(std::make_tuple(1,2,3)) << std::endl;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement