Last active
July 21, 2019 08:45
-
-
Save deni64k/6077048dba21f92b7b70d3f1c614462d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <tuple> | |
template <typename T> | |
std::ostream& operator << (std::ostream& os, T const& obj) { | |
using std::operator <<; | |
std::apply([&os](auto const& fst, auto const&... rest) { | |
os << fst; | |
((os << ", " << rest), ...); | |
}, obj.members()); | |
return os; | |
} | |
struct employee { | |
std::string name; | |
int salary; | |
auto members() noexcept { | |
return std::forward_as_tuple(name, salary); | |
} | |
auto members() const noexcept { | |
return std::forward_as_tuple(name, salary); | |
} | |
}; | |
int main(int, char**) { | |
employee e{"Steve Jobs", 1}; | |
std::cout << e << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment