fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4.  
  5. template <typename T>
  6. struct base_type {};
  7.  
  8. template <>
  9. struct base_type<int> {
  10. static std::string get_type() {return "int";}
  11. };
  12.  
  13. template <>
  14. struct base_type<float> {
  15. static std::string get_type() {return "float";}
  16. };
  17.  
  18. // write more specializations for primitive and pointers and arrays, not important here
  19.  
  20.  
  21. struct ubiq_lref {
  22. std::string& out;
  23. std::string dummy;
  24. ubiq_lref(const int&_): out(dummy) {}
  25. ubiq_lref(std::string &x, const int& _): out(x) {}
  26.  
  27. template <typename T>
  28. operator T () {
  29. out += base_type<T>::get_type() + ", ";
  30. return T();
  31. }
  32. };
  33.  
  34.  
  35.  
  36. template <typename T, int ... ints>
  37. struct reflect {
  38. static std::string get_members() {
  39. std::string s = "{";
  40. T{ubiq_lref(s, ints)...};
  41. s += "}";
  42. return s;
  43. }
  44. };
  45.  
  46. template <typename T, int ... ints>
  47. auto get_str(int) ->typename std::enable_if<std::is_same<decltype(T{ubiq_lref(ints)...}), T>::value, std::string>::type {
  48. return reflect<T, ints...>::get_members();
  49. }
  50.  
  51. template <typename T, int a, int...ints>
  52. auto get_str(long) -> std::string {
  53. return get_str<T, ints...>(0);
  54. }
  55.  
  56.  
  57. template <typename T, int...ints>
  58. std::string get_members(std::integer_sequence<int, ints...> _) {
  59. return get_str<T, ints...>(0);
  60. }
  61.  
  62.  
  63. struct foo {
  64. int x;
  65. float y;
  66. int z;
  67. };
  68.  
  69.  
  70. int main() {
  71. // your code goes here
  72. std::cout << get_members<foo>(std::make_integer_sequence<int, (int)sizeof(foo)>()) << std::endl;
  73. return 0;
  74. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
{int, float, int, }