看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《tropical72 (藍影)》之銘言: : 但.是 (如果沒有但是的話下面可以不用看了...) : 但.是. 如果好死不死,那些 func1 , func2 的 prototype 長得不一樣的話呢? : 像是 void func1(int, int) , double func2(double, int) , ... etc , : 這裡其實我覺得還是直接用 45 個 switch-case 下去硬幹會比較方便, : 走 C 風格的話還是可以用 函式指標陣列,只是所有函式必須改寫成 : void func(void *) 或是 int func( void *) ,至於 C++ 有沒有比較好的解法? : 嗯,我想是有的,只是我也沒用過,也想知道。 當然是可以的,如果寫不出來一定是TMP用得不夠用力 (誤 https://gist.github.com/PkmX/0fa65c755c39b676d79e (請搭配 gcc-5.1 / clang-3.6 -std=c++1z 服用) void f1(const int x) { std::cout << "f1: " << x << std::endl; } void f2(const double x) { std::cout << "f2: " << x << std::endl; } index_call(0, std::make_tuple(42), f1, f2); // f1(42) index_call(1, std::make_tuple(42), f1, f2); // f2(42) 這裡index_call的第一個參數是代表要call第幾個function, 而第二個參數是要傳給該function的參數們,用一個std::tuple包起來, 後面可以接若干個callback objects 注意到上面f1和f2的參數型態不一樣,但是還可以放在一起選擇呼叫 ---- std::string f3(const char x) { /* ... */ } const auto v1 = index_call(2, std::make_tuple(42), f1, f2, f3); // f3(42) 這裡多了一點變化,f3會回傳一個std::string, 而index_call所return的type會是所有function的回傳型態的集合, 用一個boost::variant包起來,所以上面v1的型態就是: const boost::variant<std::string, std::tuple<>> std::tuple<>是用來代表回傳型態為void的function的一個dummy type,可以忽略 因此我們可以用boost::get把v1內f3(42)回傳的std::string給拿出來 boost::get<std::string>(v1); ---- 當然塞lambda也是可以的: // decltype(v2) == const boost::variant<std::string, int, std::tuple<>> const auto v2 = index_call(1, std::make_tuple(), std::bind(f3, 0), [] { std::cout << "lambda" << std::endl; return 42; }); std::cout << boost::get<int>(v2) << std::endl; // 42 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.88.55 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1433321265.A.993.html
BlazarArc: 太over了XDDD 06/03 17:00
Feis: 不過我個人比較喜歡 func_set[key](..arguments) 這樣的用法 06/03 17:06
Feis: index_call 感覺很新潮~ 06/03 17:08
PkmX: 把index_call稍微包裝一下就有你想要的syntax了 06/03 18:06
loveme00835: 新手路過問下, 如果有兩個functor具相同回傳型別呢? 06/03 22:39
loveme00835: 卻要取用其中一個的回傳值 06/03 22:41
loveme00835: 阿沒事.. 有點混亂 ^^ 06/03 22:48
tropical72: 好猛! vs 版本不夠新的話似乎也只能乾瞪眼 ? @@ 06/04 11:27
johnhmj: cin.clear, cin.sync, cin.get 06/10 23:55