结构体反射可以从动态的地址获取这个结构体成员的名字不 #1065
Unanswered
icyfox168168
asked this question in
Q&A
Replies: 3 comments
-
|
可以这样做: constexpr auto idx = index_of<&test::id>();
constexpr auto arr = get_member_names<test>();
constexpr std::string_view name = arr[idx];
static_assert(name == "id"); |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
这个好像只能编译期的静态地址吧?我记得应该有个存偏移量的map,应该去那里面动态查
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
要运行期的这样就可以了: constexpr auto arr = get_member_names<test>();
auto i = index_of(&test::id);
auto ss = arr[i];
assert(ss == "id"); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
struct test {
int id;
};
我传入 &test.id 这个地址可以直接返回std::string id 这个字符串不
Beta Was this translation helpful? Give feedback.
All reactions