- C++相談室 part139
583 :デフォルトの名無しさん (ワッチョイ 899f-QT7o)[sage]:2018/12/08(土) 13:35:08.63 ID:cc77YSwU0 - using FUNC = std::function<void(Params&)>;
struct Params {}; template<> std::tuple<> convert_params(const Params&) { return {}; } template<> std::tuple<int> convert_params(Params& p) { return 0; } template<typename... Args> inline static auto message_handler(void(*func)(Args...)) { return [=](Params& m) { std::apply(func, convert_params<std::tuple<Args...>>(p)); }; } template<typename... Args> inline static auto message_handler(std::function<void(Args...)> func) { return [=](Params& m) { std::apply(func, convert_params<std::tuple<Args...>>(p)); }; } template<typename T, typename... Args> inline static auto message_handler(void(T::*func)(Args...), T* obj) { assert(obj != nullptr); return [=](Params& m) { std::apply(func, std::tuple_cat( std::tuple<T&>(*obj), convert_params<std::tuple<Args...>>(p)) ); }; }
| - C++相談室 part139
584 :デフォルトの名無しさん (ワッチョイ 899f-QT7o)[sage]:2018/12/08(土) 13:35:36.54 ID:cc77YSwU0 - ↑の続き
前の投稿で定義した関数を使い、下記の通りに呼び出そうとしているのですが std::functionを通さずにラムダ直で呼び出した場合に、なんとなく上手にやってくれる 方法がどうしても思いつけません。何か手はないでしょうか? 1 void myfunction(int); FUNC func = message_handler(myfunction); // OK グローバル関数 2 void myclass::myfunction(int); FUNC func = message_handler(myfunction, this); // OK メンバ関数 3 FUNC func = message_handler([](int){}); // NG ラムダ直 4 std::function<void(int)> myfunction = [](int){}; FUNC func = message_handler(myfunction); // OK std::function
| - C++相談室 part139
586 :デフォルトの名無しさん (ワッチョイ 899f-QT7o)[sage]:2018/12/08(土) 14:25:14.54 ID:cc77YSwU0 - >>585
あれ、マジでうまくいきました。 調べ方がいまいちわからないのですが、これはどういった機能でしょうか?
| - C++相談室 part139
587 :デフォルトの名無しさん (ワッチョイ 899f-QT7o)[sage]:2018/12/08(土) 14:29:45.48 ID:cc77YSwU0 - すみません、お礼を言い忘れています。
ありがとうございます。
|
|