typedef std::map argMap; template struct Caster; template<> struct Caster<> { static const int something = 0; int setup( const argMap &m, int arg ) { return arg; } }; template void assignThing( T **thing, void *with ) { *thing = (T *)with; } template void assignThing( T *thing, void *with ) { } template struct Caster { typedef typename boost::mpl::if_< boost::is_reference, typename boost::add_pointer::type >::type, T >::type NonRefType; NonRefType a; Caster b; int setup( argMap &m, int arg = 0 ) { if ( boost::is_reference::value ) { std::cout << "Reference argument" << std::endl; assignThing( &a, (m[arg]).asVoid() ); } else { std::cout << "Fetching arg #" << arg << std::endl; if ( !m.count( arg ) ) throw std::string( "Wrong number of args" ); a = (m[arg]).getValue(); std::cout << "Left the fetching function, set to " << TypeName::getValue() << " - " << a << std::endl; } return b.setup( m, ++arg ); } }; template struct argsToAnyDataP; template<> struct argsToAnyDataP<> { static const int num = 0; static const int deepest = 0; static int argGetter( int n, argMap &m ) { return 0; } }; template struct argsToAnyDataP { static int argGetter( int n, argMap &m, const T& arg, const Args&... args ) { argsToAnyDataP::argGetter( n+1, m, args... ); m[ n ] = arg; } }; template AnyData AnyData::operator()(const Args&... args) { typedef AnyData (*fptr)(void *, argMap &); fptr f = (fptr)callerFunc; argMap m; argsToAnyDataP::argGetter( 0, m, args... ); //std::cout << "I am here.." << std::endl; return f( getValue(), m ); } template AnyData theCaller( void * funcToCall, argMap &m ) //, const Args... a ) { std::cout << "I am in the caller." << sizeof...( Args) << std::endl; Caster them; if ( them.setup( m ) != m.size() ) throw std::string("Wrong number of arguments"); typedef RET (*fptr)( Caster ); std::cout << "On my way out.." << std::endl; return AnyData( ((fptr)funcToCall)( them ) ); } template void AnyData::setCallerFunc( AnyData (*p)(const Args...) ) { callerFunc = (void *)p; } template AnyData &AnyData::operator=( RET (*assign)( Args... ) ) { typedef __typeof(assign) funcp; //Args...; std::cout << "Assigning function pointer..." << std::endl; funcArgs = functionGetter(); setCallerFunc( &theCaller ); //theCaller( 1, 2 ); delete data; data = new Storage( assign ); return *this; }