Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <tuple>
- struct Employee {
- Employee(std::size_t age, const std::string &name, const std::string &secondName)
- : m_age{age},
- m_name{name},
- m_secondName{secondName}{
- }
- std::size_t& getAge() { return m_age; }
- std::string& getName() { return m_name; }
- std::string& getSecondName(){ return m_secondName; }
- private:
- std::size_t m_age;
- std::string m_name;
- std::string m_secondName;
- };
- namespace std {
- template<>
- struct tuple_size<Employee> {
- static constexpr size_t value = 3;
- };
- template<>
- struct tuple_element<0, Employee> {
- using type = size_t;
- };
- template<>
- struct tuple_element<1, Employee> {
- using type = std::string;
- };
- template<>
- struct tuple_element<2, Employee> {
- using type = std::string;
- };
- }
- template<size_t Index>
- decltype(auto) get(Employee &employee) {
- if constexpr (Index == 0)
- return employee.getAge();
- else if (Index == 1)
- return employee.getName();
- else if (Index == 2)
- return employee.getSecondName();
- static_assert(Index >= 0 && Index <= 2, "Something wrong!");
- };
- void func_employee() {
- std::cout << "\nfunc_employee\n";
- Employee employee{42, "Petia", "Ivanofff"};
- auto&[age, name, secondName] = employee;
- std::cout << "age = " << age << " name = " << name << " secondName = " << secondName << std::endl;
- age = 43;
- std::cout << "age = " << employee.getAge() << " name = " << employee.getName() << " secondName = " << employee.getSecondName() << std::endl;
- }
- int main() {
- func_employee();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement