#include "../../cmm/dispatch.h" #include #include struct Shape { virtual ~Shape() {} }; struct Square : Shape { }; struct Triangle : Shape { }; bool Overlap( virtual Shape&, virtual Shape&); bool Overlap_( static Square& /*a*/, static Triangle& /*b*/) { std::cerr << "Overlap_( Square&, Triangle&) called\n"; return false; } bool Overlap_( static Triangle& /*a*/, static Square& /*b*/) { std::cerr << "Overlap_( Triangle&, Square&) called\n"; return false; } bool Overlap_( static Shape& /*a*/, static Square& /*b*/) { std::cerr << "Overlap_( Shape&, Square&) called\n"; return false; } bool Overlap_( static Square& /*b*/, static Shape& /*a*/) { std::cerr << "Overlap_( Square&, Shape&) called\n"; return false; } namespace { void Show( std::exception& e) { std::cerr << "\nCaught exception:\n" << e.what() << "\n"; } } int main() { try { Shape& s = *new Square; Shape& t = *new Triangle; try { Overlap( t, t); // Throws - no matching implementation throw std::runtime_error( "Overlap( t, t) should have thrown"); } catch( cmm_exception_unmatched& e) { std::cerr << "caught expected exception: " << e.what() << "\n"; } try { Overlap( s, t); // Calls Overlap_( Square&, Triangle&) } catch( std::exception& e) { throw std::runtime_error( "Overlap( s, t) threw, but should have been successful"); } try { Overlap( t, s); // Calls Overlap_( Triangle&, Square&) } catch( std::exception& e) { throw std::runtime_error( "Overlap( t, s) threw, but should have been successful"); } try { Overlap( s, s); // Throws - no best matching implementation throw std::runtime_error( "Overlap( s, t) should have thrown"); } catch( cmm_exception_ambiguous& e) { std::cerr << "caught expected exception: " << e.what() << "\n"; } return 0; } catch( std::exception& e) { std::cerr << "Error: " << e.what() << "\n"; abort(); } return 0; }