fork(2) download
  1. #include <iostream>
  2. #include <cstdint>
  3.  
  4. namespace so
  5. {
  6. template <typename Ta, typename Tb, typename Tc, std::size_t =
  7. ((sizeof(Ta) >= sizeof(Tb)) && (sizeof(Tb) >= sizeof(Tc))) ? 10 :
  8. ((sizeof(Ta) >= sizeof(Tc)) && (sizeof(Tc) >= sizeof(Tb))) ? 11 :
  9. ((sizeof(Tb) >= sizeof(Ta)) && (sizeof(Ta) >= sizeof(Tc))) ? 20 :
  10. ((sizeof(Tb) >= sizeof(Tc)) && (sizeof(Tc) >= sizeof(Ta))) ? 21 :
  11. ((sizeof(Tc) >= sizeof(Ta)) && (sizeof(Ta) >= sizeof(Tb))) ? 30 :
  12. ((sizeof(Tc) >= sizeof(Tb)) && (sizeof(Tb) >= sizeof(Ta))) ? 31 : 0>
  13. struct foo {};
  14.  
  15. template <typename Ta, typename Tb, typename Tc>
  16. struct foo<Ta, Tb, Tc, 10>
  17. {
  18. Ta a;
  19. Tb b;
  20. Tc c;
  21. foo(Ta _a, Tb _b, Tc _c) : a{_a}, b{_b}, c{_c} {}
  22. };
  23.  
  24. template <typename Ta, typename Tb, typename Tc>
  25. struct foo<Ta, Tb, Tc, 11>
  26. {
  27. Ta a;
  28. Tc c;
  29. Tb b;
  30. foo(Ta _a, Tb _b, Tc _c) : a{_a}, c{_c}, b{_b} {}
  31. };
  32.  
  33. template <typename Ta, typename Tb, typename Tc>
  34. struct foo<Ta, Tb, Tc, 20>
  35. {
  36. Tb b;
  37. Ta a;
  38. Tc c;
  39. foo(Ta _a, Tb _b, Tc _c) : b{_b}, a{_a}, c{_c} {}
  40. };
  41.  
  42. template <typename Ta, typename Tb, typename Tc>
  43. struct foo<Ta, Tb, Tc, 21>
  44. {
  45. Tb b;
  46. Tc c;
  47. Ta a;
  48. foo(Ta _a, Tb _b, Tc _c) : b{_b}, c{_c}, a{_a} {}
  49. };
  50.  
  51. template <typename Ta, typename Tb, typename Tc>
  52. struct foo<Ta, Tb, Tc, 30>
  53. {
  54. Tc c;
  55. Ta a;
  56. Tb b;
  57. foo(Ta _a, Tb _b, Tc _c) : c{_c}, a{_a}, b{_b} {}
  58. };
  59.  
  60. template <typename Ta, typename Tb, typename Tc>
  61. struct foo<Ta, Tb, Tc, 31>
  62. {
  63. Tc c;
  64. Tb b;
  65. Ta a;
  66. foo(Ta _a, Tb _b, Tc _c) : c{_c}, b{_b}, a{_a} {}
  67. };
  68.  
  69. template <typename Ta, typename Tb, typename Tc>
  70. struct bar: public foo<Ta, Tb, Tc>
  71. {
  72. private:
  73. using base = foo<Ta, Tb, Tc>;
  74. public:
  75. bar() = default;
  76. using base::base;
  77. };
  78.  
  79. template <typename Ta, typename Tb, typename Tc>
  80. struct foobar
  81. {
  82. Ta a;
  83. Tb b;
  84. Tc c;
  85. foobar() = default;
  86. foobar(Ta _a, Tb _b, Tc _c) : a{_a}, b{_b}, c{_c} {}
  87. };
  88. } //namespace so
  89.  
  90. int main()
  91. {
  92. so::bar<std::uint16_t, std::uint32_t, std::uint16_t> bar{1, 2, 3};
  93. so::foobar<std::uint16_t, std::uint32_t, std::uint16_t> foobar{1, 2, 3};
  94.  
  95. std::cout << sizeof(bar) << "\t" << sizeof(foobar) << std::endl;
  96.  
  97. std::cout << bar.a << " : " << bar.b << " : " << bar.c << std::endl;
  98. std::cout << foobar.a << " : " << foobar.b << " : " << foobar.c << std::endl;
  99.  
  100. return (0);
  101. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
8	12
1 : 2 : 3
1 : 2 : 3