#include <iostream>
#include <tuple>
#include <string>
using namespace std;
// basically C++14's integer sequence
template < int ...>
struct seq { } ;
template < int N, int ...S >
struct gens : gens< N- 1 , N- 1 , S...> { } ;
template < int ...S >
struct gens< 0 , S...> {
typedef seq< S...> type;
} ;
template < typename R, typename A, typename ...B , int ...S > auto
map_1( R ( * f) ( A) , std:: tuple < B...> && t, seq< S...> )
- > decltype( std:: make_tuple ( f( std:: get < S> ( t) ) ...) )
{
return std:: make_tuple ( f( std:: get < S> ( t) ) ...) ;
}
template < typename R, typename A, typename ...B > auto
map( R ( * f) ( A) , std:: tuple < B...> && t)
- > decltype( map_1( f, std:: forward < std:: tuple < B...>> ( t) , typename gens< sizeof ...( B) > :: type ( ) ) )
{
return map_1( f, std:: forward < std:: tuple < B...>> ( t) , typename gens< sizeof ...( B) > :: type ( ) ) ;
}
// Some functions
double f( double x) { return x; }
size_t f( std:: string s) { return s.size ( ) ; }
#define BINDOVERLOADED(X) struct{template<class T>auto operator()(T&&t)const->decltype(X(std::forward<T>(t)){return X(std::forward<T>(t));}}()
int main( ) {
map( BINDOVERLOADED( f) , make_tuple( 3.14 , "a string" ) ) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dHVwbGU+CiNpbmNsdWRlIDxzdHJpbmc+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgoKLy8gYmFzaWNhbGx5IEMrKzE0J3MgaW50ZWdlciBzZXF1ZW5jZQp0ZW1wbGF0ZTxpbnQgLi4uPgpzdHJ1Y3Qgc2VxIHsgfTsKCnRlbXBsYXRlPGludCBOLCBpbnQgLi4uUz4Kc3RydWN0IGdlbnMgOiBnZW5zPE4tMSwgTi0xLCBTLi4uPiB7IH07Cgp0ZW1wbGF0ZTxpbnQgLi4uUz4Kc3RydWN0IGdlbnM8MCwgUy4uLj4gewogIHR5cGVkZWYgc2VxPFMuLi4+IHR5cGU7Cn07Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBSLCB0eX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gc3RyaW5nIikpOwoJcmV0dXJuIDA7Cn0=
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