2
#include <tuple>

using namespace std;

int f(int a, int b)
{
    return a + b;
}

int main() 
{    
    auto t = make_tuple(1, 2);

    return f(magic_xxx(t)); // How to implement magic_xxx?
}

我想要的是f(magic_xxx(t))等同于f(1, 2),如何实现?

4

1 回答 1

1

一种方法:

#define TUPLE_VALUES_1(t) std::get<0>(t)
#define TUPLE_VALUES_2(t) TUPLE_VALUES_1(t), std::get<1>(t)
#define TUPLE_VALUES_3(t) TUPLE_VALUES_2(t), std::get<2>(t)


f(TUPLE_VALUES_2(t));

更新

来自 TC 评论的链接有一个更好的解决方案。通过在它周围添加一个包装函数,您可以获得您所希望的。

#include <tuple>
#include <iostream>
using std::cout;
using std::endl;

// --------------------
// BEGIN borrowed code 
// --------------------
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 Ret, typename ...Args>
struct Dispatcher
{
   template<int ...S>
   Ret callFunc(seq<S...>)
   {
      return func_(std::get<S>(params_) ...);
   }

   std::tuple<Args...> params_;
   Ret (*func_)(Args...);
};
// --------------------
// END borrowed code 
// --------------------

// Wrapper function
template <typename Ret, typename ...Args>
Ret callFunc(std::tuple<Args...> t, Ret (*func)(Args...))
{
   Dispatcher<Ret, Args...> disp = {t, func};
   return disp.callFunc(typename gens<sizeof...(Args)>::type());
}

// ---------------------
// BEGIN Test functions
// ---------------------

double foo(int x, float y, double z)
{
   return x + y + z;
}

int bar(int a, int b)
{
   return a + b;
}

void baz(int a)
{
}

void test1()
{
  auto t = std::make_tuple<int, float, double>(1, 1.2, 5);
  cout << callFunc(t, foo) << endl;
}

void test2()
{
   auto t = std::make_tuple<int, int>(1, 2);
   cout << callFunc(t, bar) << endl;
}

void test3()
{
   auto t = std::make_tuple<int>(2);
   callFunc(t, baz);
}
// ---------------------
// END Test functions
// ---------------------

int main(void)
{
   // Test the functionality
   test1();
   test2();
   test3();
}
于 2014-07-09T15:48:21.600 回答