オブザーバーの特定のメンバー関数にパラメーターを完全に転送できるオブザーバー パターンを再作成しようとしています。
複数のオーバーライドを持つメンバー関数のアドレスを渡そうとすると、引数に基づいて正しいメンバー関数を推測できません。
#include <iostream>
#include <vector>
#include <algorithm>
template<typename Class>
struct observer_list
{
template<typename Ret, typename... Args, typename... UArgs>
void call(Ret (Class::*func)(Args...), UArgs&&... args)
{
for (auto obj : _observers)
{
(obj->*func)(std::forward<UArgs>(args)...);
}
}
std::vector<Class*> _observers;
};
struct foo
{
void func(const std::string& s)
{
std::cout << this << ": " << s << std::endl;
}
void func(const double d)
{
std::cout << this << ": " << d << std::endl;
}
};
int main()
{
observer_list<foo> l;
foo f1, f2;
l._observers = { &f1, &f2 };
l.call(&foo::func, "hello");
l.call(&foo::func, 0.5);
return 0;
}
これは でコンパイルできませんtemplate argument deduction/substitution failed
。
関数シグネチャの型と同じ型である必要はありませんが、その型に変換できるパラメーターを渡すことができる必要があるためArgs...
、 andがあったことに注意してください。UArgs...
あいまいさを解消するために呼び出しを使用できるとstd::enable_if<std::is_convertible<Args, UArgs>>
考えていましたが、可変個引数テンプレート パラメーター パックでこれを実行できるとは思いませんか?
ここでテンプレート引数の推論を機能させるにはどうすればよいですか?