#include "../../cmm/dispatch.h" // so we can catch cmm-specific exceptions. #include #include #include struct Shape { virtual ~Shape() {} }; struct Square : Shape { }; struct Triangle : Shape { }; bool Overlap( double x, virtual const Shape&, int y, virtual Shape&); bool Overlap_( double, static const Square& /*a*/, int, static Triangle& /*b*/) { std::cerr << "Overlap_( Square&, Triangle&) called\n"; return false; } bool Overlap_( double, static const Triangle& /*a*/, int, static Square& /*b*/) { std::cerr << "Overlap_( Triangle&, Square&) called\n"; return false; } bool Overlap_( double, static const Shape& /*a*/, int, static Square& /*b*/) { std::cerr << "Overlap_( Shape&, Square&) called\n"; return false; } bool Overlap_( double, static const Square& /*b*/, int, static Shape& /*a*/) { std::cerr << "Overlap_( Square&, Shape&) called\n"; return false; } namespace { void Show( std::exception& e) { std::cerr << "\nCaught expected exception, type " << typeid( e).name() << ":\n"; std::cerr << e.what() << "\n"; } } int main() { try { Shape& a = *new Square; Shape& b = *new Triangle; try { Overlap( 0, b, 0, b); abort(); } // Throws - no matching implementation catch( cmm_exception_unmatched& e) { Show( e); } Overlap( 0, a, 0, b); // Calls Overlap_( Square&, Triangle&) Overlap( 0, b, 0, a); // Calls Overlap_( Triangle&, Square&) try { Overlap( 0, a, 0, a); abort(); } // Throws - no best matching implementation catch( cmm_exception_ambiguous& e) { Show( e); } } catch( ...) { std::cerr << "Unexpected exception. calling abort()\n"; abort(); } return 0; }