fork download
  1. #include <utility>
  2.  
  3. template< typename BinaryFunc, typename First, typename... Types >
  4. struct helper;
  5.  
  6. template< typename BinaryFunc, typename First>
  7. struct helper<BinaryFunc, First> {
  8. typedef decltype(std::declval<First>()) type;
  9. };
  10.  
  11. template< typename BinaryFunc, typename First, typename Second >
  12. struct helper<BinaryFunc, First, Second> {
  13. typedef decltype(
  14. std::declval<BinaryFunc>()( std::declval<First>(), std::declval<Second>() )
  15. ) type;
  16. };
  17.  
  18. template< typename BinaryFunc, typename First, typename Second, typename... Rest >
  19. struct helper<BinaryFunc, First, Second, Rest...> {
  20. typedef typename helper< BinaryFunc,
  21. typename helper<BinaryFunc, First, Second>::type,
  22. Rest...
  23. >::type
  24. type;
  25. };
  26.  
  27. template< typename BinaryFunc, typename First, typename Second >
  28. typename helper<BinaryFunc, First, Second>::type
  29. foldl( BinaryFunc&& func, First&& first, Second&& second ) {
  30. return func( std::forward<First>(first), std::forward<Second>(second) );
  31. }
  32.  
  33. template< typename BinaryFunc, typename First, typename Second, typename... Rest >
  34. typename helper<BinaryFunc, First, Second, Rest...>::type
  35. foldl( BinaryFunc&& func, First&& first, Second&& second, Rest&&... rest ) {
  36. return foldl(
  37. std::forward<BinaryFunc>(func),
  38. func( std::forward<First>(first), std::forward<Second>(second) ),
  39. std::forward<Rest>(rest)...
  40. );
  41. }
  42.  
  43. struct adder
  44. {
  45. template< int LHS, int RHS >
  46. std::integral_constant<int,LHS+RHS>
  47. operator()( std::integral_constant<int,LHS>, std::integral_constant<int,RHS> )
  48. {
  49. return {};
  50. }
  51. };
  52.  
  53. int main() {
  54. foldl( adder(),
  55. std::integral_constant<int,1>{},
  56. std::integral_constant<int,2>{},
  57. std::integral_constant<int,3>{},
  58. std::integral_constant<int,4>{}
  59. );
  60. return 0;
  61. }
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty