#include <tuple>
#include <utility>
namespace detail
{
template<int... Is>
struct seq { };
template<int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template<int... Is>
struct gen_seq<0, Is...> : seq<Is...> { };
template<typename F, typename... Ts, int... Is>
void call_with_tuple(F&& f, std::tuple<Ts...> const& t, seq<Is...>)
{
(std::forward<F>(f))(std::get<Is>(t)...);
}
}
template<typename F, typename... Ts>
void call_with_tuple(F&& f, std::tuple<Ts...> const& t)
{
detail::call_with_tuple(std::forward<F>(f), t, detail::gen_seq<sizeof...(Ts)>());
}
#include <iostream>
#include <functional>
template< typename ... Args >
class Message {
public:
Message( Args&& ... args ) {
mArgs = std::make_tuple( args ... );
}
std::tuple< Args ... > mArgs;
typedef std::function< void ( Args ... ) > HandlerType;
void Consume( HandlerType handler ) {
call_with_tuple(handler, mArgs);
}
};
int main()
{
// Testing code
Message<int, int> msg(1, 2);
msg.Consume( [] ( int i, int j ) {
std::cout << i << ',' << j << '\n';
});
}
I2luY2x1ZGUgPHR1cGxlPgojaW5jbHVkZSA8dXRpbGl0eT4KCm5hbWVzcGFjZSBkZXRhaWwKewogICAgdGVtcGxhdGU8aW50Li4uIElzPgogICAgc3RydWN0IHNlcSB7IH07CiAgICAKICAgIHRlbXBsYXRlPGludCBOLCBpbnQuLi4gSXM+CiAgICBzdHJ1Y3QgZ2VuX3NlcSA6IGdlbl9zZXE8TiAtIDEsIE4gLSAxLCBJcy4uLj4geyB9OwogICAgCiAgICB0ZW1wbGF0ZTxpbnQuLi4gSXM+CiAgICBzdHJ1Y3QgZ2VuX3NlcTwwLCBJcy4uLj4gOiBzZXE8SXMuLi4+IHsgfTsKICAgIAogICAgdGVtcGxhdGU8dHlwZW5hbWUgRiwgdHlwZW5hbWUuLi4gVHMsIGludC4uLiBJcz4KICAgIHZvaWQgY2FsbF93aXRoX3R1cGxlKEYmJiBmLCBzdGQ6OnR1cGxlPFRzLi4uPiBjb25zdCYgdCwgc2VxPElzLi4uPikKICAgIHsKIC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