#include #include #include struct Either_Error : public std::runtime_error { using std::runtime_error::runtime_error; }; int main() { // test basic functionality { Either> e { "hello" }; assert( e.has_first() ); assert( e.get_first() == "hello" ); e = "a kinda long string to make sure memory is allocated"; assert( e.get_first() == "a kinda long string to make sure memory is allocated" ); // Make sure the invalid version throws an exception try { e.get_second(); assert(false); } catch (Either_Error&) { } e = std::vector { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; assert( !e.has_first() ); assert(( e.get_second() == std::vector { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } )); // Make sure the invalid version throws an exception try { e.get_first(); assert(false); } catch (Either_Error&) { assert(true); } e = "Another string"; assert( e.has_first() ); assert( e.get_first() == "Another string" ); } // test destructors { std::vector v { 1, 2, 3, 4, 5, 6, 7, 8 }; Either, std::string> e1 { v }; assert( e1.get_first() == v ); std::string s { "a longer string which must take some space, thus ignoring short strings" }; Either, std::string> e2 { s }; assert( e2.get_second() == s ); } }