具有可变函数参数的C++多态性
C++ polymorphism with variadic function parameter
我将与您分享一个使用变函数参数的类的问题。它是以下代码中显示的类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/元组: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线程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线程::operator()(Args…)[带有Args={int}]
我真的不理解这个错误,因为纯虚拟函数在被嘲笑的类中定义得很好。然而,在将函数run()移动到派生类(Functor)中时,它是有效的。
提前感谢,Caner
根据[thread.thread.contr]§3,std::thread
构造函数的第一个参数类型为F&&
,要求F
为MoveConstructible
。在您的情况下,F
是Thread
,而不是MoveConstructible
。
换句话说,std::thread
需要按值存储函子,而您将函子转发为Thread
,这是抽象的。
问题是:
std::forward< Thread<Args...> >(*this)
其试图复制CCD_ 10子对象。幸运的是,它是抽象的,所以您会得到编译错误,而不是意外的运行时行为。
您需要一个引用包装器:
std::ref(*this)
我考虑了参与者对此主题提供的多项建议,包括std::ref
的使用,并希望与您分享解决我在前一代码中遇到的问题的工作代码版本。
#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::ref(*this), args...);
t.join();
}
};
template<class... Args>
class Functor : public Thread<Args...>
{
public:
void operator()(int)
{
while (1)
{
cout << "42 "; cout.flush();
}
}
};
int main()
{
int a = 12;
Functor<int> f;
f.run(ref(a));
return 0;
}
再次感谢。
相关文章:
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 如何调用指针类型的方法(禁用多态性)?
- 从基类调用函数的多态性
- 运行时多态性 - 箭头运算符访问了错误的成员?
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?