C++ 反射

参考链接:

http://pfultz2.com/blog/2012/07/31/reflection-in-under-100-lines/

https://zhuanlan.zhihu.com/p/165993590 & https://github.com/netcan/recipes/tree/master/cpp/metaproggramming

https://netcan.github.io/2020/08/01/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E5%AE%9E%E7%8E%B0C-%E7%BC%96%E8%AF%91%E6%9C%9F%E9%9D%99%E6%80%81%E5%8F%8D%E5%B0%84/ & https://zhuanlan.zhihu.com/p/158147380 & http://startheap.com/2018/08/04/Metaprogramming-and-reflection-mechanisms/ & https://github.com/Ubpa/USRefl 

purecpp:http://www.purecpp.org/detail?id=1120 & http://purecpp.org/detail?id=2071

https://www.cnblogs.com/heleifz/p/cpp-template-reflection.html 

https://www.jianshu.com/p/674ed466db4c

在线编码器:https://godbolt.org/

1. 类型成员个数统计 

// g++ count_memer.cpp --std=c++11 -Wall -Werror
#include <cstdio>

template <typename... Ts>
struct make_void {
  typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;

// AnyType声明了类型转换操作符(《C++ Modern
// design》书中的术语是稻草人函数),可以转换成任意类型
struct AnyType {
  template <typename T>
  operator T();
};

template <typename T, typename = void, typename... Ts>
struct CountMember {
  constexpr static size_t value = sizeof...(Ts) - 1;
};

// 用decltype(T{Ts{}...})来判断是否能够构造对象T
template <typename T, typename... Ts>
struct CountMember<T, void_t<decltype(T{Ts{}...})>, Ts...> {
  constexpr static size_t value = CountMember<T, void, Ts..., AnyType>::value;
};

int main(int argc, char** argv) {
  struct Test {
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    unsigned int aa = 0;
    char zjr = 0;
  };
  printf("%zu
", CountMember<Test>::value);
}
View Code

2. 

原文地址:https://www.cnblogs.com/zengjianrong/p/14525028.html