Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <tuple>
  5.  
  6. struct Employee {
  7. Employee(std::size_t age, const std::string &name, const std::string &secondName)
  8. : m_age{age},
  9. m_name{name},
  10. m_secondName{secondName}{
  11.  
  12. }
  13.  
  14. std::size_t& getAge() { return m_age; }
  15.  
  16. std::string& getName() { return m_name; }
  17.  
  18. std::string& getSecondName(){ return m_secondName; }
  19.  
  20.  
  21. private:
  22. std::size_t m_age;
  23. std::string m_name;
  24. std::string m_secondName;
  25.  
  26. };
  27.  
  28. namespace std {
  29. template<>
  30. struct tuple_size<Employee> {
  31. static constexpr size_t value = 3;
  32. };
  33. template<>
  34. struct tuple_element<0, Employee> {
  35. using type = size_t;
  36. };
  37.  
  38. template<>
  39. struct tuple_element<1, Employee> {
  40. using type = std::string;
  41. };
  42.  
  43. template<>
  44. struct tuple_element<2, Employee> {
  45. using type = std::string;
  46. };
  47.  
  48. }
  49.  
  50. template<size_t Index>
  51. decltype(auto) get(Employee &employee) {
  52. if constexpr (Index == 0)
  53. return employee.getAge();
  54. else if (Index == 1)
  55. return employee.getName();
  56. else if (Index == 2)
  57. return employee.getSecondName();
  58.  
  59. static_assert(Index >= 0 && Index <= 2, "Something wrong!");
  60. };
  61.  
  62. void func_employee() {
  63. std::cout << "\nfunc_employee\n";
  64. Employee employee{42, "Petia", "Ivanofff"};
  65. auto&[age, name, secondName] = employee;
  66. std::cout << "age = " << age << " name = " << name << " secondName = " << secondName << std::endl;
  67. age = 43;
  68. std::cout << "age = " << employee.getAge() << " name = " << employee.getName() << " secondName = " << employee.getSecondName() << std::endl;
  69. }
  70.  
  71. int main() {
  72. func_employee();
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement