サブクラスメソッドが存在する場合、それを呼び出す必要がある init() メソッドを持つテンプレートクラスが 1 つあります。Base クラスのメソッド init() は永久に呼び出します。
template <class T>
class Base
{
template<typename... Args>
void init(Args... args);
T subj;
explicit Base() { subj = new T(); }
}
template<typename... Args>
Base<T>::init(Args... args)
{
invoke_if_exists<&T::init,subj>(args); // <-- that moment
}
invoke_if_exists テンプレートを実装する必要があります。アルゴリズムはそのようなコードであるべきです
if ( method_exists(T::init) )
{
subj->init(Args...);
}
テンプレートにラップする必要があります
どうもありがとう。
[アップデート]:
少し広く説明してみましょう。
class Foo
{
// ... and there is no init()
};
class Right
{
void init() { ... }
/// ...
}
auto a = new Base<Foo>();
auto b = new Base<Right>();
a->init(); // there is no call for Foo::init();
b->init(); // there is correct call for Right::init();
invoke_if_exists<>
init() メソッドだけでなく、任意のメソッドを使用したい。