#include #include #include #include class type { public: enum type_code { Bool, Int, String, Pointer, None }; std::string as_string() const; type(bool b2) : we_are(Bool), b(b2), i(), s(), p(0) {} type(int i2) : we_are(Int), b(), i(i2), s(), p(0) {} type(std::string s2) : we_are(String), b(b), i(), s(s2), p(0) {} type(char* s2) : we_are(String), b(b), i(), s(s2), p(0) {} type(void* p2) : we_are(Pointer), b(b), i(), s(), p(p2) {} type() : we_are(None), b(), i(), s(), p(0) {} private: type_code we_are; bool b; int i; std::string s; const void* p; }; std::string type::as_string() const { std::stringstream buf; switch(we_are) { case None: buf << "nil"; break; case Int: buf << "int(" << i << ")"; break; case String: buf << "string(\"" << s << "\")"; break; case Pointer: buf << "pointer(" << p << ")"; break; case Bool: buf << "bool(" << (b ? "true" : "false") << ")"; break; default: buf << ""; } return buf.str(); } std::ostream& operator<<(std::ostream& out, const type& t) { out << t.as_string(); return out; } int main() { std::map types; types["type(nil)"] = type(); types["type(true)"] = type(true); types["type(false)"] = type(false); types["type(bool())"] = type(bool()); types["type(int())"] = type(int()); types["type(0)"] = type(0); types["type((void*)0)"] = type((void*)0); types["type((int*)0)"] = type((int*)0); types["type(1001)"] = type(1001); types["type(\"foo\")"] = type("foo"); types["type(std::string(\"bar\"))"] = type(std::string("bar")); types["type(&types)"] = type(&types); std::map::const_iterator j; for(j = types.begin(); j != types.end(); j++) std::cout << j->first << " is " << j->second << std::endl; }