草庐IT

c++ - 将 std::function<void(Derived*)> 转换为 std::function<void(Base*)>

首先,我定义了两个类,它们相互继承。classA{};classB:publicA{};然后,我声明一个使用std::function的函数:voiduseCallback(std::functionmyCallback);最后,我收到了std::function与我想在回调函数中使用的其他地方不同(但理论上兼容)的类型:std::functionthisIsAGivenFunction;useCallback(thisIsAGivenFunction);我的编译器(clang++)拒绝这样做,因为thisIsAGivenFunction的类型与预期的类型不匹配。但与B继承自A,这对t

c++ - 将 std::function<void(Derived*)> 转换为 std::function<void(Base*)>

首先,我定义了两个类,它们相互继承。classA{};classB:publicA{};然后,我声明一个使用std::function的函数:voiduseCallback(std::functionmyCallback);最后,我收到了std::function与我想在回调函数中使用的其他地方不同(但理论上兼容)的类型:std::functionthisIsAGivenFunction;useCallback(thisIsAGivenFunction);我的编译器(clang++)拒绝这样做,因为thisIsAGivenFunction的类型与预期的类型不匹配。但与B继承自A,这对t

c++ - GCC/VS2008 : Different behaviour of function call when templated base class is derived from itself

以下代码适用于VisualStudio2008,但不适用于GCC/G++4.3.420090804。根据C++标准,哪种行为正确?templatestructA:A{};templatestructA{};structB:A{};templatevoidFunc(constA&a){}intmain(){Aa;//isderivedfromAFunc(a);//vs2008:ok,g++:ok//Comeau:okBb;//isderivedfromAFunc(b);//vs2008:ok,g++:error,nomatchingfunctionforcalltoFunc(B&)//C

c++ - GCC/VS2008 : Different behaviour of function call when templated base class is derived from itself

以下代码适用于VisualStudio2008,但不适用于GCC/G++4.3.420090804。根据C++标准,哪种行为正确?templatestructA:A{};templatestructA{};structB:A{};templatevoidFunc(constA&a){}intmain(){Aa;//isderivedfromAFunc(a);//vs2008:ok,g++:ok//Comeau:okBb;//isderivedfromAFunc(b);//vs2008:ok,g++:error,nomatchingfunctionforcalltoFunc(B&)//C

C++:错误 "... is not derived from type ..."

templateclassBimap{public:classData;typedefData*DataP;typedefstd::multimapT1Map;typedefstd::multimapT2Map;classData{private:Bimap&bimap;T1Map::iteratorit1;/*...*/};};这给了我这个编译错误:error:type'std::multimap::Data*,std::less,std::allocator::Data*>>>'isnotderivedfromtype'Bimap::Data'这是什么意思?这里有什么问题?

C++:错误 "... is not derived from type ..."

templateclassBimap{public:classData;typedefData*DataP;typedefstd::multimapT1Map;typedefstd::multimapT2Map;classData{private:Bimap&bimap;T1Map::iteratorit1;/*...*/};};这给了我这个编译错误:error:type'std::multimap::Data*,std::less,std::allocator::Data*>>>'isnotderivedfromtype'Bimap::Data'这是什么意思?这里有什么问题?

c++ - 试图从 Derived* 的 vector 中分配 Base* 的 vector

这似乎是一个非常基本的问题,但我无法弄清楚。我有一个std::vector指向派生对象的原始指针,我只想使用赋值运算符将它复制到另一个基指针vector。使用VC++我得到错误C2679"binary'=':nooperatorfound..."BTW我不想要对象的深层拷贝,我只想复制指针。示例代码:#includeusingnamespacestd;structBase{};structDerived:publicBase{};intmain(intargc,char*argv[]){vectorV1;vectorV2;V2=V1;//Compilererrorherereturn0

c++ - 试图从 Derived* 的 vector 中分配 Base* 的 vector

这似乎是一个非常基本的问题,但我无法弄清楚。我有一个std::vector指向派生对象的原始指针,我只想使用赋值运算符将它复制到另一个基指针vector。使用VC++我得到错误C2679"binary'=':nooperatorfound..."BTW我不想要对象的深层拷贝,我只想复制指针。示例代码:#includeusingnamespacestd;structBase{};structDerived:publicBase{};intmain(intargc,char*argv[]){vectorV1;vectorV2;V2=V1;//Compilererrorherereturn0

c++ - gcc和clang的重载解析差异涉及move构造函数和 'Derived(Base&&)'构造函数

GCC(用4.9测试)接受以下测试用例:structBase{};structDerived:Base{Derived();explicitDerived(constDerived&);explicitDerived(Derived&&);explicitDerived(constBase&);Derived(Base&&);};Derivedfoo(){Derivedresult;returnresult;}intmain(){Derivedresult=foo();}Clang(用3.5测试)拒绝它并显示以下错误消息:test.cpp:13:10:error:nomatchingc

c++ - gcc和clang的重载解析差异涉及move构造函数和 'Derived(Base&&)'构造函数

GCC(用4.9测试)接受以下测试用例:structBase{};structDerived:Base{Derived();explicitDerived(constDerived&);explicitDerived(Derived&&);explicitDerived(constBase&);Derived(Base&&);};Derivedfoo(){Derivedresult;returnresult;}intmain(){Derivedresult=foo();}Clang(用3.5测试)拒绝它并显示以下错误消息:test.cpp:13:10:error:nomatchingc