#include <cassert>
#include <cstdio>
// Abstracte Interfaces ueber DLL-Grenze per Id zuordnen - so eine Art COM-Interface fuer arme Leute
// abstract Base
struct Base
{
};
// abstract Interfaces
struct InterfaceA{};
struct InterfaceB{};
struct InterfaceC{};
// Interface-Type zu Id mapping
template<typename Type> struct InterfaceId {};
template<> struct InterfaceId<InterfaceA>{ static constexpr int id = 0; };
template<> struct InterfaceId<InterfaceB>{ static constexpr int id = 1; };
template<> struct InterfaceId<InterfaceC>{ static constexpr int id = 2; };
template<typename... Interface>
struct Test: Base, Interface...
{
void typed_throw(int id)
{
// <-- wie kann man dieses If mit C++17 aus dem Interface-Parameter pack generieren lassen?
if( id == InterfaceId<InterfaceA>::id ) throw static_cast<InterfaceA*>(this);
if( id == InterfaceId<InterfaceB>::id ) throw static_cast<InterfaceC*>(this);
throw this;
}
};
int main()
{
Test<InterfaceA,InterfaceC> t;
try {
t.typed_throw(InterfaceId<InterfaceA>::id);
}
catch(InterfaceA* e) { printf("cached A\n"); }
try {
t.typed_throw(InterfaceId<InterfaceC>::id);
}
catch(InterfaceC* e) { printf("cached C\n"); }
return 0;
}