#include <exception>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
struct Base {};
struct Derived : Base {};
struct Derived2 : Base {};
struct Ambiguous : Derived, Derived2 {};
struct Private : private Base {};
struct Protected : protected Base {};
template <typename T
,typename E
,typename O
>
void assert_catches()
{
try
{
O o;
throw static_cast<E>(&o);
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Statements after throw must be unreachable");
}
catch (T t)
{
assert(true);
return;
}
catch (...)
{
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Should not have entered catch-all");
}
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "The catch should have returned");
}
template <typename T
,typename E
,typename O
>
void assert_cannot_catch()
{
try
{
O o;
throw static_cast<E>(&o);
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Statements after throw must be unreachable");
}
catch (T t)
{
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "Should not have entered the catch");
}
catch (...)
{
assert(true);
return;
}
printf("%s\n", __PRETTY_FUNCTION__);
assert(false && "The catch-all should have returned");
}
void f1()
{
assert_catches< Base * , Derived *, Derived>();
assert_catches<const Base * , Derived *, Derived>();
assert_catches< volatile Base * , Derived *, Derived>();
assert_catches<const volatile Base * , Derived *, Derived>();
assert_catches< Base * const , Derived *, Derived>();
assert_catches<const Base * const , Derived *, Derived>();
assert_catches< volatile Base * const , Derived *, Derived>();
assert_catches<const volatile Base * const , Derived *, Derived>();
assert_catches< Base * volatile, Derived *, Derived>();
assert_catches<const Base * volatile, Derived *, Derived>();
assert_catches< volatile Base * volatile, Derived *, Derived>();
assert_catches<const volatile Base * volatile, Derived *, Derived>();
assert_catches< Base * const volatile, Derived *, Derived>();
assert_catches<const Base * const volatile, Derived *, Derived>();
assert_catches< volatile Base * const volatile, Derived *, Derived>();
assert_catches<const volatile Base * const volatile, Derived *, Derived>();
}
void f2()
{
assert_catches< Base * , Base *, Derived>();
assert_catches<const Base * , Base *, Derived>();
assert_catches< volatile Base * , Base *, Derived>();
assert_catches<const volatile Base * , Base *, Derived>();
assert_catches< Base * const , Base *, Derived>();
assert_catches<const Base * const , Base *, Derived>();
assert_catches< volatile Base * const , Base *, Derived>();
assert_catches<const volatile Base * const , Base *, Derived>();
assert_catches< Base * volatile, Base *, Derived>();
assert_catches<const Base * volatile, Base *, Derived>();
assert_catches< volatile Base * volatile, Base *, Derived>();
assert_catches<const volatile Base * volatile, Base *, Derived>();
assert_catches< Base * const volatile, Base *, Derived>();
assert_catches<const Base * const volatile, Base *, Derived>();
assert_catches< volatile Base * const volatile, Base *, Derived>();
assert_catches<const volatile Base * const volatile, Base *, Derived>();
}
void f3()
{
assert_catches< Derived * , Derived *, Derived>();
assert_catches<const Derived * , Derived *, Derived>();
assert_catches< volatile Derived * , Derived *, Derived>();
assert_catches<const volatile Derived * , Derived *, Derived>();
assert_catches< Derived * const , Derived *, Derived>();
assert_catches<const Derived * const , Derived *, Derived>();
assert_catches< volatile Derived * const , Derived *, Derived>();
assert_catches<const volatile Derived * const , Derived *, Derived>();
assert_catches< Derived * volatile, Derived *, Derived>();
assert_catches<const Derived * volatile, Derived *, Derived>();
assert_catches< volatile Derived * volatile, Derived *, Derived>();
assert_catches<const volatile Derived * volatile, Derived *, Derived>();
assert_catches< Derived * const volatile, Derived *, Derived>();
assert_catches<const Derived * const volatile, Derived *, Derived>();
assert_catches< volatile Derived * const volatile, Derived *, Derived>();
assert_catches<const volatile Derived * const volatile, Derived *, Derived>();
}
void f4()
{
assert_cannot_catch< Derived * , Base *, Derived>();
assert_cannot_catch<const Derived * , Base *, Derived>();
assert_cannot_catch< volatile Derived * , Base *, Derived>();
assert_cannot_catch<const volatile Derived * , Base *, Derived>();
assert_cannot_catch< Derived * const , Base *, Derived>();
assert_cannot_catch<const Derived * const , Base *, Derived>();
assert_cannot_catch< volatile Derived * const , Base *, Derived>();
assert_cannot_catch<const volatile Derived * const , Base *, Derived>();
assert_cannot_catch< Derived * volatile, Base *, Derived>();
assert_cannot_catch<const Derived * volatile, Base *, Derived>();
assert_cannot_catch< volatile Derived * volatile, Base *, Derived>();
assert_cannot_catch<const volatile Derived * volatile, Base *, Derived>();
assert_cannot_catch< Derived * const volatile, Base *, Derived>();
assert_cannot_catch<const Derived * const volatile, Base *, Derived>();
assert_cannot_catch< volatile Derived * const volatile, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const volatile, Base *, Derived>();
}
void f5()
{
assert_catches< Derived * &, Derived *, Derived>();
assert_catches<const Derived * &, Derived *, Derived>();
assert_catches< volatile Derived * &, Derived *, Derived>();
assert_catches<const volatile Derived * &, Derived *, Derived>();
assert_catches< Derived * const &, Derived *, Derived>();
assert_catches<const Derived * const &, Derived *, Derived>();
assert_catches< volatile Derived * const &, Derived *, Derived>();
assert_catches<const volatile Derived * const &, Derived *, Derived>();
assert_catches< Derived * volatile &, Derived *, Derived>();
assert_catches<const Derived * volatile &, Derived *, Derived>();
assert_catches< volatile Derived * volatile &, Derived *, Derived>();
assert_catches<const volatile Derived * volatile &, Derived *, Derived>();
assert_catches< Derived * const volatile &, Derived *, Derived>();
assert_catches<const Derived * const volatile &, Derived *, Derived>();
assert_catches< volatile Derived * const volatile &, Derived *, Derived>();
assert_catches<const volatile Derived * const volatile &, Derived *, Derived>();
}
void f6()
{
assert_catches< Base * &, Base *, Derived>();
assert_catches<const Base * &, Base *, Derived>();
assert_catches< volatile Base * &, Base *, Derived>();
assert_catches<const volatile Base * &, Base *, Derived>();
assert_catches< Base * const &, Base *, Derived>();
assert_catches<const Base * const &, Base *, Derived>();
assert_catches< volatile Base * const &, Base *, Derived>();
assert_catches<const volatile Base * const &, Base *, Derived>();
assert_catches< Base * volatile &, Base *, Derived>();
assert_catches<const Base * volatile &, Base *, Derived>();
assert_catches< volatile Base * volatile &, Base *, Derived>();
assert_catches<const volatile Base * volatile &, Base *, Derived>();
assert_catches< Base * const volatile &, Base *, Derived>();
assert_catches<const Base * const volatile &, Base *, Derived>();
assert_catches< volatile Base * const volatile &, Base *, Derived>();
assert_catches<const volatile Base * const volatile &, Base *, Derived>();
}
void f7()
{
assert_cannot_catch< Derived * &, Base *, Derived>();
assert_cannot_catch<const Derived * &, Base *, Derived>();
assert_cannot_catch< volatile Derived * &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * &, Base *, Derived>();
assert_cannot_catch< Derived * const &, Base *, Derived>();
assert_cannot_catch<const Derived * const &, Base *, Derived>();
assert_cannot_catch< volatile Derived * const &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const &, Base *, Derived>();
assert_cannot_catch< Derived * volatile &, Base *, Derived>();
assert_cannot_catch<const Derived * volatile &, Base *, Derived>();
assert_cannot_catch< volatile Derived * volatile &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * volatile &, Base *, Derived>();
assert_cannot_catch< Derived * const volatile &, Base *, Derived>();
assert_cannot_catch<const Derived * const volatile &, Base *, Derived>();
assert_cannot_catch< volatile Derived * const volatile &, Base *, Derived>();
assert_cannot_catch<const volatile Derived * const volatile &, Base *, Derived>();
}
void f8()
{
assert_catches< Base * &, Derived *, Derived>();
assert_catches<const Base * &, Derived *, Derived>();
assert_catches< volatile Base * &, Derived *, Derived>();
assert_catches<const volatile Base * &, Derived *, Derived>();
assert_catches< Base * const &, Derived *, Derived>();
assert_catches<const Base * const &, Derived *, Derived>();
assert_catches< volatile Base * const &, Derived *, Derived>();
assert_catches<const volatile Base * const &, Derived *, Derived>();
assert_catches< Base * volatile &, Derived *, Derived>();
assert_catches<const Base * volatile &, Derived *, Derived>();
assert_catches< volatile Base * volatile &, Derived *, Derived>();
assert_catches<const volatile Base * volatile &, Derived *, Derived>();
assert_catches< Base * const volatile &, Derived *, Derived>();
assert_catches<const Base * const volatile &, Derived *, Derived>();
assert_catches< volatile Base * const volatile &, Derived *, Derived>();
assert_catches<const volatile Base * const volatile &, Derived *, Derived>();
}
void f9()
{
assert_cannot_catch< Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * , Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * const , Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch< volatile Base * const volatile, Ambiguous *, Ambiguous>();
assert_cannot_catch<const volatile Base * const volatile, Ambiguous *, Ambiguous>();
}
void f10()
{
assert_cannot_catch< Base * , Private *, Private>();
assert_cannot_catch<const Base * , Private *, Private>();
assert_cannot_catch< volatile Base * , Private *, Private>();
assert_cannot_catch<const volatile Base * , Private *, Private>();
assert_cannot_catch< Base * const , Private *, Private>();
assert_cannot_catch<const Base * const , Private *, Private>();
assert_cannot_catch< volatile Base * const , Private *, Private>();
assert_cannot_catch<const volatile Base * const , Private *, Private>();
assert_cannot_catch< Base * volatile, Private *, Private>();
assert_cannot_catch<const Base * volatile, Private *, Private>();
assert_cannot_catch< volatile Base * volatile, Private *, Private>();
assert_cannot_catch<const volatile Base * volatile, Private *, Private>();
assert_cannot_catch< Base * const volatile, Private *, Private>();
assert_cannot_catch<const Base * const volatile, Private *, Private>();
assert_cannot_catch< volatile Base * const volatile, Private *, Private>();
assert_cannot_catch<const volatile Base * const volatile, Private *, Private>();
}
void f11()
{
assert_cannot_catch< Base * , Protected *, Protected>();
assert_cannot_catch<const Base * , Protected *, Protected>();
assert_cannot_catch< volatile Base * , Protected *, Protected>();
assert_cannot_catch<const volatile Base * , Protected *, Protected>();
assert_cannot_catch< Base * const , Protected *, Protected>();
assert_cannot_catch<const Base * const , Protected *, Protected>();
assert_cannot_catch< volatile Base * const , Protected *, Protected>();
assert_cannot_catch<const volatile Base * const , Protected *, Protected>();
assert_cannot_catch< Base * volatile, Protected *, Protected>();
assert_cannot_catch<const Base * volatile, Protected *, Protected>();
assert_cannot_catch< volatile Base * volatile, Protected *, Protected>();
assert_cannot_catch<const volatile Base * volatile, Protected *, Protected>();
assert_cannot_catch< Base * const volatile, Protected *, Protected>();
assert_cannot_catch<const Base * const volatile, Protected *, Protected>();
assert_cannot_catch< volatile Base * const volatile, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const volatile, Protected *, Protected>();
}
void f12()
{
assert_cannot_catch< Base * &, Private *, Private>();
assert_cannot_catch<const Base * &, Private *, Private>();
assert_cannot_catch< volatile Base * &, Private *, Private>();
assert_cannot_catch<const volatile Base * &, Private *, Private>();
assert_cannot_catch< Base * const &, Private *, Private>();
assert_cannot_catch<const Base * const &, Private *, Private>();
assert_cannot_catch< volatile Base * const &, Private *, Private>();
assert_cannot_catch<const volatile Base * const &, Private *, Private>();
assert_cannot_catch< Base * volatile &, Private *, Private>();
assert_cannot_catch<const Base * volatile &, Private *, Private>();
assert_cannot_catch< volatile Base * volatile &, Private *, Private>();
assert_cannot_catch<const volatile Base * volatile &, Private *, Private>();
assert_cannot_catch< Base * const volatile &, Private *, Private>();
assert_cannot_catch<const Base * const volatile &, Private *, Private>();
assert_cannot_catch< volatile Base * const volatile &, Private *, Private>();
assert_cannot_catch<const volatile Base * const volatile &, Private *, Private>();
}
void f13()
{
assert_cannot_catch< Base * &, Protected *, Protected>();
assert_cannot_catch<const Base * &, Protected *, Protected>();
assert_cannot_catch< volatile Base * &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * &, Protected *, Protected>();
assert_cannot_catch< Base * const &, Protected *, Protected>();
assert_cannot_catch<const Base * const &, Protected *, Protected>();
assert_cannot_catch< volatile Base * const &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const &, Protected *, Protected>();
assert_cannot_catch< Base * volatile &, Protected *, Protected>();
assert_cannot_catch<const Base * volatile &, Protected *, Protected>();
assert_cannot_catch< volatile Base * volatile &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * volatile &, Protected *, Protected>();
assert_cannot_catch< Base * const volatile &, Protected *, Protected>();
assert_cannot_catch<const Base * const volatile &, Protected *, Protected>();
assert_cannot_catch< volatile Base * const volatile &, Protected *, Protected>();
assert_cannot_catch<const volatile Base * const volatile &, Protected *, Protected>();
}
int main(int, char**)
{
f1();
f2();
f3();
f4();
f5();
f6();
f7();
f8();
f9();
f10();
f11();
f12();
f13();
return 0;
}