fork download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. // basically C++14's integer sequence
  8. template<int ...>
  9. struct seq { };
  10.  
  11. template<int N, int ...S>
  12. struct gens : gens<N-1, N-1, S...> { };
  13.  
  14. template<int ...S>
  15. struct gens<0, S...> {
  16. typedef seq<S...> type;
  17. };
  18.  
  19. template<typename R, typename A, typename ...B, int ...S> auto
  20. map_1(R (*f)(A), std::tuple<B...> &&t, seq<S...>)
  21. -> decltype(std::make_tuple(f(std::get<S>(t))...))
  22. {
  23. return std::make_tuple(f(std::get<S>(t))...);
  24. }
  25.  
  26. template<typename R, typename A, typename ...B> auto
  27. map(R (*f)(A), std::tuple<B...> &&t)
  28. -> decltype(map_1(f, std::forward<std::tuple<B...>>(t), typename gens<sizeof...(B)>::type()))
  29. {
  30. return map_1(f, std::forward<std::tuple<B...>>(t), typename gens<sizeof...(B)>::type());
  31. }
  32.  
  33.  
  34. // Some functions
  35. double f(double x) { return x; }
  36. size_t f(std::string s) { return s.size(); }
  37.  
  38. #define BINDOVERLOADED(X) struct{template<class T>auto operator()(T&&t)const->decltype(X(std::forward<T>(t)){return X(std::forward<T>(t));}}()
  39. int main() {
  40.  
  41. map(BINDOVERLOADED(f), make_tuple(3.14, "a string"));
  42. return 0;
  43. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:38:27: error: expected primary-expression before ‘struct’
 #define BINDOVERLOADED(X) struct{template<class T>auto operator()(T&&t)const->decltype(X(std::forward<T>(t)){return X(std::forward<T>(t));}}()
                           ^
prog.cpp:41:6: note: in expansion of macro ‘BINDOVERLOADED’
  map(BINDOVERLOADED(f), make_tuple(3.14, "a string"));
      ^
stdout
Standard output is empty