#include <string>
#include <iostream>
struct Foo {
int a = 1;
double d = 2;
};
template<typename Visitor>
void visit( Visitor &v, const Foo &data )
{
v.visit( "a", data.a );
v.visit( "d", data.d );
}
struct OstreamVisitor {
OstreamVisitor( std::ostream &os ) : m_os( os ) {}
void visit( const char *name, int i ) { m_os << name << "=" << i << std::endl; }
void visit( const char *name, double d ) { m_os << name << "=" << d << std::endl; }
std::ostream &m_os;
};
std::ostream &operator<<( std::ostream &out, const Foo &data )
{
OstreamVisitor v( out );
visit( v, data );
return out;
}
int main()
{
Foo f;
std::cout << f;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKc3RydWN0IEZvbyB7CiAgICBpbnQgYSA9IDE7CiAgICBkb3VibGUgZCA9IDI7Cn07Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBWaXNpdG9yPgp2b2lkIHZpc2l0KCBWaXNpdG9yICZ2LCBjb25zdCBGb28gJmRhdGEgKQp7CiAgICB2LnZpc2l0KCAiYSIsIGRhdGEuYSApOwogICAgdi52aXNpdCggImQiLCBkYXRhLmQgKTsKfQoKc3RydWN0IE9zdHJlYW1WaXNpdG9yIHsKICAgIE9zdHJlYW1WaXNpdG9yKCBzdGQ6Om9zdHJlYW0gJm9zICkgOiBtX29zKCBvcyApIHt9CgogICAgdm9pZCB2aXNpdCggY29uc3QgY2hhciAqbmFtZSwgaW50IGkgKSB7IG1fb3MgPDwgbmFtZSA8PCAiPSIgPDwgaSA8PCBzdGQ6OmVuZGw7IH0KICAgIHZvaWQgdmlzaXQoIGNvbnN0IGNoYXIgKm5hbWUsIGRvdWJsZSBkICkgeyBtX29zIDw8IG5hbWUgPDwgIj0iIDw8IGQgPDwgc3RkOjplbmRsOyB9CgogICAgc3RkOjpvc3RyZWFtICZtX29zOwp9OwoKc3RkOjpvc3RyZWFtICZvcGVyYXRvcjw8KCBzdGQ6Om9zdHJlYW0gJm91dCwgY29uc3QgRm9vICZkYXRhICkKewogICAgT3N0cmVhbVZpc2l0b3Igdiggb3V0ICk7CiAgICB2aXNpdCggdiwgZGF0YSApOwogICAgcmV0dXJuIG91dDsKfQoKaW50IG1haW4oKQp7CiAgICBGb28gZjsKICAgIHN0ZDo6Y291dCA8PCBmOwoKfQo=