- 論壇徽章:
- 44
|
本帖最后由 windoze 于 2015-07-06 15:58 編輯
回復 29# yulihua49
如果你能把數(shù)據(jù)類型限制在有限的幾個種類,用type switch能簡單點。
比如:
- // @SEE: https://github.com/windoze/cxxstuff
- #include <cxutil/variant.hpp>
- typedef cxutil::variant<int,double,std::string> DBVariant;
- struct RDB_set_col_visitor {
- RDB_insert_visitor(DB_cursor *cursor, String column);
- int operator()(int x) &&;
- int operator()(double x) && ;
- int operator()(const std::string &x) &&;
- };
- DBVariant var(123.45);
- var.apply_visitor(RDB_set_col_visitor(cursor, "some_col"));
復制代碼 不過你想把一個struct直接映射到一個Row還是沒什么好辦法。
C++目前完全沒有任何辦法遍歷任意一個struct的成員,所以我在想用clang的compiler plugin寫個代碼生成器,實現(xiàn)類似Rust的Macro的功能。
|
|