可変引数関数パラメーターを使用するクラスで発生した問題を共有しています。次のコードに示すクラス Thread です。関数パターンを使用するための std::thread のラッパーです。
クラス Thread を新しいクラス Functor に継承する際に、この関数でポリモーフィズムを使用したかったのですが、gcc は以下のエラーを返します。
#include <thread>
#include <iostream>
using namespace std;
template<class... Args>
class Thread
{
public:
virtual void operator()(Args...) = 0;
void run(Args... args)
{
std::thread t(std::forward< Thread<Args...> >(*this), std::forward<Args>(args)...);
t.join();
}
};
template<class... Args>
class Functor : public Thread<Args...>
{
public:
// generates the errors bellow
virtual void operator()(Args... /*args*/)
{
}
// doesnot work since the pure virtual function wants another prototype of function.
// void operator()(int)
// {
// }
};
int main()
{
int a = 12;
Functor<int> f;
f.run(ref(a));
return 0;
}
t-Thread-args2.cpp:1 から: /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple: 'struct std::_Head_base のインスタンス化で、false>': /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:215:12: 'struct std から必要::_Tuple_impl, int>' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:507:11: 'class std から必要::tuple, int>' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/functional:1601:39: 'struct std から必要::_Bind_simple(int)>' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/thread:133:9: 'std から必要: :thread::thread(_Callable&&, _Args&& ...) [with _Callable = Thread; _Args = {int}]' t-Thread-args2.cpp:14:83: 'void Thread::run(Args ...) [with Args = {int}]' から必要です t-Thread-args2.cpp:42:17: ここから必要 /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/tuple:166:13: エラー: フィールドを宣言できません'std::_Head_base, false>::_M_head_impl' は抽象型 'Thread' になります t-Thread-args2.cpp:7:7: 注: 次の仮想関数は「スレッド」内で純粋であるため: t-Thread-args2.cpp:10:18: 注: void Thread::operator()(Args ...) [with Args = {int}]
純粋仮想関数は派生クラスで適切に定義されているため、エラーを本当に理解していません。ただし、関数 run() を派生クラス (Functor) に移動すると機能します。
前もってありがとう、カナー