梵蒂冈之花
这是我的代码,如果有人感兴趣的话基本上,在编译时,编译器将递归地展开各种包含函数调用<N>->调用<N-1>->调用的所有参数.->调用<0>这是最后一个调用,编译器将优化各种中间函数调用,只保留与func等效的最后一个函数(arg1、arg 2、arg3、.)提供了两个版本,一个用于调用对象上的函数,另一个用于静态函数。#include <tr1/tuple>/**
* Object Function Tuple Argument Unpacking
*
* This recursive template unpacks the tuple parameters into
* variadic template arguments until we reach the count of 0 where the function
* is called with the correct parameters
*
* @tparam N Number of tuple arguments to unroll
*
* @ingroup g_util_tuple
*/template < uint N >struct apply_obj_func{
template < typename T, typename... ArgsF, typename... ArgsT, typename... Args >
static void applyTuple( T* pObj,
void (T::*f)( ArgsF... ),
const std::tr1::tuple<ArgsT...>& t,
Args... args )
{
apply_obj_func<N-1>::applyTuple( pObj, f, t, std::tr1::get<N-1>( t ), args... );
}};//-----------------------------------------------------------------------------/**
* Object Function Tuple Argument Unpacking End Point
*
* This recursive template unpacks the tuple parameters into
* variadic template arguments until we reach the count of 0 where the function
* is called with the correct parameters
*
* @ingroup g_util_tuple
*/template <>struct apply_obj_func<0>{
template < typename T, typename... ArgsF, typename... ArgsT, typename... Args >
static void applyTuple( T* pObj,
void (T::*f)( ArgsF... ),
const std::tr1::tuple<ArgsT...>& /* t */,
Args... args )
{
(pObj->*f)( args... );
}};//-----------------------------------------------------------------------------/**
* Object Function Call Forwarding Using Tuple Pack Parameters
*/// Actual apply functiontemplate < typename T, typename... ArgsF, typename... ArgsT >void applyTuple( T* pObj,
void (T::*f)( ArgsF... ),
std::tr1::tuple<ArgsT...> const& t ){
apply_obj_func<sizeof...(ArgsT)>::applyTuple( pObj, f, t );}//-----------------------------------------------------------------------------/**
* Static Function Tuple Argument Unpacking
*
* This recursive template unpacks the tuple parameters into
* variadic template arguments until we reach the count of 0 where the function
* is called with the correct parameters
*
* @tparam N Number of tuple arguments to unroll
*
* @ingroup g_util_tuple
*/template < uint N >struct apply_func{
template < typename... ArgsF, typename... ArgsT, typename... Args >
static void applyTuple( void (*f)( ArgsF... ),
const std::tr1::tuple<ArgsT...>& t,
Args... args )
{
apply_func<N-1>::applyTuple( f, t, std::tr1::get<N-1>( t ), args... );
}};//-----------------------------------------------------------------------------/**
* Static Function Tuple Argument Unpacking End Point
*
* This recursive template unpacks the tuple parameters into
* variadic template arguments until we reach the count of 0 where the function
* is called with the correct parameters
*
* @ingroup g_util_tuple
*/template <>struct apply_func<0>{
template < typename... ArgsF, typename... ArgsT, typename... Args >
static void applyTuple( void (*f)( ArgsF... ),
const std::tr1::tuple<ArgsT...>& /* t */,
Args... args )
{
f( args... );
}};//-----------------------------------------------------------------------------/**
* Static Function Call Forwarding Using Tuple Pack Parameters
*/// Actual apply functiontemplate < typename... ArgsF, typename... ArgsT >void applyTuple( void (*f)(ArgsF...),
std::tr1::tuple<ArgsT...> const& t ){
apply_func<sizeof...(ArgsT)>::applyTuple( f, t );}// ***************************************// Usage// ***************************************template < typename T, typename... Args >class Message : public IMessage{
typedef void (T::*F)( Args... args );public:
Message( const std::string& name,
T& obj,
F pFunc,
Args... args );private:
virtual void doDispatch( );
T* pObj_;
F pFunc_;
std::tr1::tuple<Args...> args_;};//-----------------------------------------------------------------------------template < typename T, typename... Args >Message<T, Args...>::Message( const std::string& name,
T& obj,
F pFunc,
Args... args ): IMessage( name ),
pObj_( &obj ),
pFunc_( pFunc ),
args_( std::forward<Args>(args)... ){}//-----------------------------------------------------------------------------template < typename T, typename... Args >void Message<T, Args...>::doDispatch( ){
try
{
applyTuple( pObj_, pFunc_, args_ );
}
catch ( std::exception& e )
{
}}
HUWWW
在C+中,有许多方法可以扩展/解压缩元组,并将这些元组元素应用到可变模板函数中。下面是一个创建索引数组的小助手类。它在模板元编程中经常使用:// ------------- UTILITY---------------template<int...> struct index_tuple{}; template<int I, typename IndexTuple, typename... Types> struct make_indexes_impl; template<int I, int... Indexes, typename T, typename ... Types> struct make_indexes_impl<I, index_tuple<Indexes...>, T, Types...> {
typedef typename make_indexes_impl<I + 1, index_tuple<Indexes..., I>, Types...>::type type; }; template<int I, int... Indexes> struct make_indexes_impl<I, index_tuple<Indexes...> > {
typedef index_tuple<Indexes...> type; }; template<typename ... Types> struct make_indexes : make_indexes_impl<0, index_tuple<>, Types...> {};现在,完成这项工作的代码并没有那么大: // ----------UNPACK TUPLE AND APPLY TO FUNCTION ---------#include <tuple>#include <iostream> using namespace std;template<class Ret, class... Args, int... Indexes > Ret apply_helper( Ret (*pf)(Args...), index_tuple< Indexes... >, tuple<Args...>&& tup) {
return pf( forward<Args>( get<Indexes>(tup))... ); } template<class Ret, class ... Args> Ret apply(Ret (*pf)(Args...), const tuple<Args...>& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), tuple<Args...>(tup));}template<class Ret, class ... Args> Ret apply(Ret (*pf)(Args...), tuple<Args...>&& tup){
return apply_helper(pf, typename make_indexes<Args...>::type(), forward<tuple<Args...>>(tup));}试验如下:// --------------------- TEST ------------------void one(int i, double d){
std::cout << "function one(" << i << ", " << d << ");\n";}int two(int i){
std::cout << "function two(" << i << ");\n";
return i;}int main(){
std::tuple<int, double> tup(23, 4.5);
apply(one, tup);
int d = apply(two, std::make_tuple(2));
return 0;}我不是其他语言的专家,但我想如果这些语言在菜单中没有这样的功能,就没有办法做到这一点。至少用C+你可以,而且我认为这不是很复杂.