第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將元組擴展為可變模板函數(shù)的參數(shù)?

如何將元組擴展為可變模板函數(shù)的參數(shù)?

C++
守著星空守著你 2019-07-01 16:41:25
考慮帶有可變模板參數(shù)的模板函數(shù)的情況:template<typename Tret, typename... T> Tret func(const T&... t);現(xiàn)在,我有一個元組t價值觀念。我怎么打電話func()使用元組值作為參數(shù)?我讀過bind()函數(shù)對象call()函數(shù),以及apply()功能在不同的一些現(xiàn)已過時的文件中。GNU GCC 4.4的實現(xiàn)似乎有一個call()函數(shù)中的bind()類,但是關于這個主題的文檔很少。有些人建議手工編寫遞歸Hack,但各種模板參數(shù)的真正價值是能夠在上述情況下使用它們。有沒有人對IS有一個解決方案,或者暗示在哪里讀到它?如何將元組擴展為可變模板函數(shù)的參數(shù)?
查看完整描述

3 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

這是我的代碼,如果有人感興趣的話

基本上,在編譯時,編譯器將遞歸地展開各種包含函數(shù)調用<N>->調用<N-1>->調用的所有參數(shù).->調用<0>這是最后一個調用,編譯器將優(yōu)化各種中間函數(shù)調用,只保留與func等效的最后一個函數(shù)(arg1、arg 2、arg3、.)

提供了兩個版本,一個用于調用對象上的函數(shù),另一個用于靜態(tài)函數(shù)。

#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 )
  {

  }}


查看完整回答
反對 回復 2019-07-01
?
HUWWW

TA貢獻1874條經(jīng)驗 獲得超12個贊

在C+中,有許多方法可以擴展/解壓縮元組,并將這些元組元素應用到可變模板函數(shù)中。下面是一個創(chuàng)建索引數(shù)組的小助手類。它在模板元編程中經(jīng)常使用:

// ------------- 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...> {};

現(xiàn)在,完成這項工作的代碼并沒有那么大:

 // ----------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+你可以,而且我認為這不是很復雜.


查看完整回答
反對 回復 2019-07-01
  • 3 回答
  • 0 關注
  • 574 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號