gpt4 book ai didi

c++ - 为什么不能将对继承自(非模板)类 B 的类模板 A 的引用转换为对 B 的引用?

转载 作者:行者123 更新时间:2023-11-28 00:53:12 24 4
gpt4 key购买 nike

我有一个像这样的类层次结构(这是实际的类,但我清理了它):

class Notifiable 
{
public:
void notify();
}

template <class Exp>
class Batch : public Notifiable
{
public:
void run();
}

void Batch<Exp>::run()
{
done.clear();
generator->resetGeneration();

while(generator->hasMoreParameters())
{
// Lock for accessing active
std::unique_lock<std::mutex> lock(q_mutex, std::adopt_lock);

// If we've less experiments than threads
if (active.size() < threads)
{
Configuration conf = generator->generateParameters();
Exp e(executable, conf);
//std::weak_ptr<Batch<Exp>> bp;
//bp.reset(this);

std::thread t(&Exp::run, e, *this);
std::thread::id id = t.get_id();
active.insert(id);
t.detach();
}
q_control.wait(lock, [this] { return active.size() < threads; } );
}
}


class Experiment
{
public:
void run(Notifiable& caller)
{
do_stuff();
caller.notify();
}

virtual void do_stuff() = 0;
}

class MyExperiment : public Experiment
{
public:
void do_stuff()
{
// do my stuff
}
}

然后我实例化一个 Batch<MyExperiment>反对并调用 run() ,使用此代码:

Batch<ELExperiment> b(pex, options["name"].as<string>(), options["executable"].as<string>());
b.run();

但我在编译时得到了这个:

In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void (Experiment::*)(Notifiable&)>, MyExperiment, Batch<MyExperiment> >':
/opt/local/include/gcc47/c++/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/functional:1563:61: required from 'struct std::_Bind_simple<std::_Mem_fn<void (Experiment::*)(Notifiable&)>(MyExperiment, Batch<MyExperiment>)>'
/opt/local/include/gcc47/c++/thread:133:9: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Experiment::*)(Notifiable&); _Args = {MyExperiment&, Batch<MyExperiment>&}]'
json2cli/batch.hh:86:46: required from 'void Batch<Exp>::run() [with Exp = MyExperiment]'
json2cli/main.cpp:113:15: required from here
/opt/local/include/gcc47/c++/type_traits:1834:9: error: no match for call to '(std::_Mem_fn<void (Experiment::*)(Notifiable&)>) (MyExperiment, Batch<MyExperiment>)'
In file included from /opt/local/include/gcc47/c++/memory:81:0,
from json2cli/parameterexpression.hh:19,
from json2cli/main.cpp:13:
/opt/local/include/gcc47/c++/functional:525:11: note: candidates are:
/opt/local/include/gcc47/c++/functional:548:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:548:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment&'
/opt/local/include/gcc47/c++/functional:553:7: note: _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:553:7: note: no known conversion for argument 1 from 'MyExperiment' to 'Experiment*'
/opt/local/include/gcc47/c++/functional:559:2: note: template<class _Tp> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&, _ArgTypes ...) const [with _Tp = _Tp; _Res = void; _Class = Experiment; _ArgTypes = {Notifiable&}]
/opt/local/include/gcc47/c++/functional:559:2: note: template argument deduction/substitution failed:
In file included from /opt/local/include/gcc47/c++/bits/move.h:57:0,
from /opt/local/include/gcc47/c++/bits/stl_pair.h:61,
from /opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from /opt/local/include/gcc47/c++/bits/char_traits.h:41,
from /opt/local/include/gcc47/c++/ios:41,
from /opt/local/include/gcc47/c++/ostream:40,
from /opt/local/include/gcc47/c++/iostream:40,
from json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits:1834:9: note: cannot convert 'std::declval<Batch<MyExperiment> >()' (type 'Batch<MyExperiment>') to type 'Notifiable&'

看来我不能指望概括任何 Batch<Exp>进入 Notifiable用于函数调用。你能证实吗?

更新 抱歉,我认为我可以避免将所有代码转储到问题中,但实际上我为 Batch<Exp>::run() 生成线程的方式肯定有问题.仍然缺少一些细节,但我真的不认为它们是相关的(例如,我如何生成实验参数)。

谢谢

最佳答案

您的错误不在您向我们展示的代码中,您在代码中的某些地方尝试绑定(bind)您的run 并使用std::thread 创建线程这就是问题所在,因为它无法为您的绑定(bind)函数创建正确的结构,最简单的解决方法是编写您自己的包装器:

template< class Expr >
struct my_bind {
my_bind( Expr& e, Notifiable& n ) : e_( e ), n_(n) {}
void operator()() {e_.run(n_);}
Expr& e_;
Notifiable& n_;
};

然后使用你自己的包装器来启动函数,我不能肯定地说但我认为这是编译器中的一个错误(所有编译器都有这个错误:GCC,MSVC,......)当你表达得到复杂的他们无法在 std::bind!!

中使用它

关于c++ - 为什么不能将对继承自(非模板)类 B 的类模板 A 的引用转换为对 B 的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com