Re: [問題] swith_case 用法
※ 引述《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
推
06/03 17:00, , 1F
06/03 17:00, 1F
→
06/03 17:06, , 2F
06/03 17:06, 2F
推
06/03 17:08, , 3F
06/03 17:08, 3F
→
06/03 18:06, , 4F
06/03 18:06, 4F
→
06/03 22:39, , 5F
06/03 22:39, 5F
→
06/03 22:41, , 6F
06/03 22:41, 6F
→
06/03 22:48, , 7F
06/03 22:48, 7F
推
06/04 11:27, , 8F
06/04 11:27, 8F
→
06/10 23:55, , 9F
06/10 23:55, 9F
討論串 (同標題文章)