fork download
  1. #include <type_traits>
  2.  
  3. /* Row */
  4. struct T_cash_account_row {
  5.  
  6. // Fields:
  7. enum T_field_enum { amount_of_money_e, gender_e, age_e, code_e, height_e, /*<<<<<- add fields here (with index) */
  8. last_e /*<<<<<- add included fields here (without index) */
  9. };
  10. static_assert(last_e > 0, "Number of indexed fields in enum of T_cash_account_row must be greater than 0!");
  11.  
  12. unsigned code:20; // 0 - 1000000
  13. unsigned gender:1; // 0 - 1
  14. unsigned age:7; // 0 - 100
  15. unsigned amount_of_money:20; // 0 - 1000000
  16. unsigned height:9; // 0 – 300
  17.  
  18.  
  19. // Get field value
  20. template<int field_enum> inline typename std::enable_if<field_enum == code_e, decltype(code)>::type get_field_value() const { return code; }
  21.  
  22. template<int field_enum> inline typename std::enable_if<field_enum == gender_e, decltype(gender)>::type get_field_value() const { return gender; }
  23.  
  24. template<int field_enum> inline typename std::enable_if<field_enum == age_e, decltype(age)>::type get_field_value() const { return age; }
  25.  
  26. template<int field_enum> inline typename std::enable_if<field_enum == amount_of_money_e, decltype(amount_of_money)>::type get_field_value() const { return amount_of_money; }
  27.  
  28. template<int field_enum> inline typename std::enable_if<field_enum == height_e, decltype(height)>::type get_field_value() const { return height; }
  29.  
  30. template<int field_enum> inline typename std::enable_if<field_enum == last_e, bool>::type get_field_value() const { return true; }
  31.  
  32. static_assert(5 == last_e, "Add/delete new field-case and correct this assert!");
  33. };
  34. /* ----------------------------------------------------------------------- */
  35.  
  36. int main() {
  37. T_cash_account_row *row = new T_cash_account_row;
  38. auto code = row->get_field_value<T_cash_account_row::code_e>();
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 2980KB
stdin
Standard input is empty
stdout
Standard output is empty