考慮帶有可變模板參數的模板函數的情況:template<typename Tret, typename... T> Tret func(const T&... t);現在,我有一個元組t價值觀念。我怎么打電話func()使用元組值作為參數?我讀過bind()函數對象call()函數,以及apply()功能在不同的一些現已過時的文件中。GNU GCC 4.4的實現似乎有一個call()函數中的bind()類,但是關于這個主題的文檔很少。有些人建議手工編寫遞歸Hack,但各種模板參數的真正價值是能夠在上述情況下使用它們。有沒有人對IS有一個解決方案,或者暗示在哪里讀到它?如何將元組擴展為可變模板函數的參數?
3 回答
梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
#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
TA貢獻1874條經驗 獲得超12個贊
// ------------- 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;}- 3 回答
- 0 關注
- 587 瀏覽
添加回答
舉報
0/150
提交
取消
