Created
March 16, 2016 14:15
-
-
Save suchasplus/9f4cbfc4652a15fb51dc to your computer and use it in GitHub Desktop.
get_class_meta_in_one_line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAKE_PAIR(text) std::pair<std::string, decltype(text)>{#text, text} | |
template<typename T> | |
constexpr static inline auto apply(T const & args) | |
{ | |
return args; | |
} | |
template<typename T, typename T1, typename... Args> | |
constexpr static inline auto apply(T const & t, const T1& first, const Args&... args) | |
{ | |
return apply(std::tuple_cat(t, std::make_tuple(MAKE_PAIR(first))), args...); | |
} | |
#define META(...) auto meta(){ return apply(std::tuple<>(), __VA_ARGS__); } | |
struct person | |
{ | |
std::string name; | |
int age; | |
META(name, age) //定义一个支持变参的meta函数 | |
}; | |
int main() | |
{ | |
person p = {“tom”, 20}; | |
auto tp = p.meta(); | |
static_assert(std::is_same(std::tuple<std::pair<std::string, int>>, decltype(tp), “not same”); | |
//tp在内存中是这样的 {{“name”:”tom”}, {“age”:20}}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment