::std::tuple<Args...>
を使用して保存する必要があります。しかし、問題は、必要な時にそれを解凍する方法です。そのためには、「インデックス」というテクニックを使用する必要があります。
ここでは、私があなたがやろうとしていることをほぼ行った場所へのリンクです。ここで一番重要なクラスは、suspended_call
です。
https://bitbucket.org/omnifarious/sparkles/src/tip/sparkles/deferred.hpp?at=default
少しだけで、私が最も関連性の高いビットを抽出し、コードの観点に入れましょう。
This line:
auto saved_args = ::std::make_tuple(::std::move(args)...);
はタプルに引数を保存します。私はそこに::std::move
を使用しました、そして、私はそれが正しいことだと思います。しかし、それは私が間違っている可能性があり、私は::std::forward
を使用する必要があります。私は、シグナリングの意図とは別の正確な違いについて明確にされていません。
実際に保存された引数で呼び出しを行うコードはhereです。コードは、私がやっていることを正確に特定しています。インデックスを実装するビットは、::std::get<I>
テンプレートを引数として使用するインデックスにマップする整数のパックを作成することを含みます。この整数のパックを取得したら、それを使用して::std::get
への呼び出しを展開し、すべてのタプル要素を個別の引数として取得できます。
私は比較的簡単な方法であることないコードを思い付くしようとするでしょう:
#include <tuple>
#include <cstddef>
#include <string>
#include <utility>
template < ::std::size_t... Indices>
struct indices {};
template < ::std::size_t N, ::std::size_t... Is>
struct build_indices : build_indices<N-1, N-1, Is...>
{};
template < ::std::size_t... Is>
struct build_indices<0, Is...> : indices<Is...>
{};
template <typename FuncT, typename ArgTuple, ::std::size_t... Indices>
auto call(const FuncT &f, ArgTuple &&args, const indices<Indices...> &)
-> decltype(f(::std::get<Indices>(::std::forward<ArgTuple>(args))...))
{
return ::std::move(f(::std::get<Indices>(::std::forward<ArgTuple>(args))...));
}
template <typename FuncT, typename ArgTuple>
auto call(const FuncT &f, ArgTuple &&args)
-> decltype(call(f, args,
build_indices< ::std::tuple_size<ArgTuple>::value>{}))
{
const build_indices< ::std::tuple_size<ArgTuple>::value> indices;
return ::std::move(call(f, ::std::move(args), indices));
}
int myfunc(::std::string name, const unsigned int foo)
{
return 0;
}
int foo(::std::tuple< ::std::string, const unsigned int> saved_args)
{
return call(myfunc, ::std::move(saved_args));
}
このコードの多くはthis page on the indices trickから借りました。
また、それはあなたの状況に少しばかり適応しなければならない種類のサンプルです。基本的にはcall(nestFunc, saved_args)
と呼んでください。
'のstd ::タプル' - また、 'のstd :: bind'と'std :: function'とすべての楽しいものです。 –
Xeo