compile time polymorphism
-
hi.
ich suche glaube ich ein policy based design, aber habe keine ahnung, welches genau.
was ich denke zu brauchen:
#include <iostream> namespace lib { struct foo_base {}; struct foo : foo_base {}; struct foo_derived : foo {}; struct default_impl { void bar() { std::cout << "nichts\n"; } void bar(foo_base&) { bar(); } void bar(foo& v) { bar((foo_base&)v); } void bar(foo_derived& v) { bar((foo&)v); } }; template< class T > void bar() { T().bar( ); T().bar( foo_base() ); T().bar( foo() ); T().bar( foo_derived() ); } } struct my_impl : lib::default_impl { using lib::default_impl::bar; void bar(lib::foo&) { std::cout << "foo\n"; } //noch andere spezialisierungen }; int main() { lib::bar<my_impl>(); /* das _will_ ich: () -> bar() -> "nichts" (foo_base) -> bar(foo_base&) -> bar() -> "nichts" (foo) -> bar(foo&) -> "foo" (foo_derived) -> bar(foo_derived&) -> bar(foo&) -> "foo" */ std::cin.get(); }
das funktioniert leider nicht:
msvc schrieb:
nichts
nichts
foo
nichtsdas geht natürlich einfach über runtime polymorphie; aber da es zur CT bekannt ist, muss es doch auch so irgendwie gehen?!
warum/wofür:
ganz speziell geht es ums exception-handling in nicht vom nutzer erzeugten threads.
das hatte ich mir in etwa(pseudocode) so gedacht:namespace lib { struct ex_h { void operator()(void* /*unhandled*/ = 0) const noexcept {} void operator()(const std::exception&) const noexcept { this->operator()(); } //bad_alloc, runtime_error, eigene_ex, ... }; template<ex_h> void foo() { try{ /**/ } /*catch(XYZ e) { ex_h()(e); }*/ catch(exception e){ ex_h()(e); } catch(...){ ex_h()(); } } template< class ex_h > void start_foo() { thread< foo<ex_h> >(); } } struct ex_h : lib::ex_h { using lib::ex_h::operator(); void operator()(void* /*unhandled*/ = 0) const noexcept { std::cerr << "!" << std::endl; } }; int main() { lib::start_foo<ex_h>(); }
bb