Skip to content

Instantly share code, notes, and snippets.

@deni64k
Last active July 21, 2019 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deni64k/6077048dba21f92b7b70d3f1c614462d to your computer and use it in GitHub Desktop.
Save deni64k/6077048dba21f92b7b70d3f1c614462d to your computer and use it in GitHub Desktop.
#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