Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';对实例化的类模板A的引用(从(非模板)类B继承)是否转换为对B的引用?_C++_Templates_Inheritance_Gcc_C++11 - Fatal编程技术网

C++ 为什么可以';对实例化的类模板A的引用(从(非模板)类B继承)是否转换为对B的引用?

C++ 为什么可以';对实例化的类模板A的引用(从(非模板)类B继承)是否转换为对B的引用?,c++,templates,inheritance,gcc,c++11,C++,Templates,Inheritance,Gcc,C++11,我有一个这样的类层次结构(这是实际的类,但我清理了它): 但我在编译时得到: 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:

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

但我在编译时得到:

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&'
在/opt/local/include/gcc47/c++/bits/move.h:57:0中包含的文件中,
从/opt/local/include/gcc47/c++/bits/stl_pair.h:61,
从/opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from/opt/local/include/gcc47/c++/bits/char_traits.h:41,
从/opt/local/include/gcc47/c++/ios:41,
从/opt/local/include/gcc47/c++/ostream:40,
从/opt/local/include/gcc47/c++/iostream:40,
来自json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits:在“struct std::_Result_of_impl”的实例化中:
/opt/local/include/gcc47/c++/type_traits:1857:12:class std::result_of'
/opt/local/include/gcc47/c++/functional:1563:61:从“struct std::_Bind_simple”中需要
/opt/local/include/gcc47/c++/thread:133:9:std::thread(_Callable&&,_Args&&&…[带_Callable=void(实验::*)(Notifiable&);_Args={MyExperiment&,Batch&}中的必填项
json2cli/batch.hh:86:46:来自“void batch::run()[with Exp=MyExperiment]”的必填项
json2cli/main.cpp:113:15:此处需要
/opt/local/include/gcc47/c++/type_traits:1834:9:错误:调用“(std::_Mem_fn)(MyExperiment,Batch)”不匹配
在/opt/local/include/gcc47/c++/memory:81:0中包含的文件中,
从json2cli/parameterexpression.hh:19,
来自json2cli/main.cpp:13:
/opt/local/include/gcc47/c++/functional:525:11:注:候选人包括:
/opt/local/include/gcc47/c++/functional:548:7:注意:_resstd::_Mem_fn::operator()(_Class&,_ArgTypes…)const[with _Res=void;_Class=Experiment;_ArgTypes={应呈报&]
/opt/local/include/gcc47/c++/functional:548:7:注意:参数1从'MyExperiment'到'Experiment&'没有已知的转换
/opt/local/include/gcc47/c++/functional:553:7:注意:_resstd::_Mem_fn::operator()(_Class*,_ArgTypes…)const[with _Res=void;_Class=Experiment;_ArgTypes={应呈报&}]
/opt/local/include/gcc47/c++/functional:553:7:注意:参数1从“MyExperiment”到“Experiment*”没有已知的转换
/opt/local/include/gcc47/c++/functional:559:2:注意:模板_resstd::_Mem_fn::operator()(_Tp&,_ArgTypes…)const[带_Tp=_Tp;_Res=void;_Class=Experiment;_ArgTypes={应呈报&}]
/opt/local/include/gcc47/c++/functional:559:2:注意:模板参数推断/替换失败:
在/opt/local/include/gcc47/c++/bits/move.h:57:0中包含的文件中,
从/opt/local/include/gcc47/c++/bits/stl_pair.h:61,
从/opt/local/include/gcc47/c++/bits/stl_algobase.h:65,
from/opt/local/include/gcc47/c++/bits/char_traits.h:41,
从/opt/local/include/gcc47/c++/ios:41,
从/opt/local/include/gcc47/c++/ostream:40,
从/opt/local/include/gcc47/c++/iostream:40,
来自json2cli/main.cpp:9:
/opt/local/include/gcc47/c++/type_traits:1834:9:注意:无法将'std::declval()'(类型'Batch')转换为'Notifiable&'type
看起来我不能期望将任何
批处理
概括为函数调用的
应报告
。你能证实吗

更新对不起,我以为我可以避免在问题中转储所有代码,但事实上,我为
Batch::run()
生成线程的方式肯定有问题。仍然缺少一些细节,但我认为它们并不相关(例如,我如何为实验生成参数)


谢谢

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

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_;
};
模板
构造我的绑定{
我的约束(Expr&e,Notifiable&n):e(e),n(n){}
void操作符()
出口和出口;
应呈报的&n;
};

然后使用您自己的包装器启动函数,我不能肯定,但我认为这是编译器中的一个错误(所有编译器都有这个错误:GCC,MSVC,…),当表达式变得复杂时,它们无法在
std::bind
中使用它

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

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_;
};
模板
构造我的绑定{
我的约束(Expr&e,Notifiable&n):e(e),n(n){}
void操作符()
出口和出口;
应呈报的&n;
};
然后使用您自己的包装器启动函数,我不能肯定,但我认为这是编译器中的一个错误(所有编译器都有这个错误:GCC,MSVC,…),当表达式变得复杂时,它们无法在
std::bind
中使用它

变化

 std::thread t(&Exp::run, e, *this);

或者,如果您真的希望线程从
*继承副本,请执行以下操作:

std::thread t([](Exp&& e, Batch&& b) { e.run(b); }, std::move(e), *this);
错误的根源(至少是消息引用的一个)是特定的
std::thread
构造函数的语义(这里我不想公开,这有点血腥)。如果您已经熟悉语义
std::bind
(它们确实有自己的怪癖),那么您可以遵循它:

std::thread t(std::bind(&Exp::run, std::move(e), std::ref(*this));
(同样,根据您的需要,
std::ref(*this)
可以替换为
*this

更改

 std::thread t(&Exp::run, e, *this);

或者如果您真的打算让线程从
*继承一个副本,那么
std::thread t(std::bind(&Exp::run, std::move(e), std::ref(*this));