草庐IT

Covariance

全部标签

c++ - 返回 shared_ptr 时如何实现协变返回类型?

usingnamespaceboost;classA{};classB:publicA{};classX{virtualshared_ptrfoo();};classY:publicX{virtualshared_ptrfoo();};返回类型不是协变的(因此它们也不是合法的),但如果我使用原始指针代替它们就会是。解决此问题的常用习语是什么(如果有的话)? 最佳答案 我认为解决方案基本上是不可能的,因为协方差取决于与智能指针不兼容的指针算法。当Y::foo返回shared_ptr对于动态调用者,它必须转换为shared_ptr使用前

c++ - 返回 shared_ptr 时如何实现协变返回类型?

usingnamespaceboost;classA{};classB:publicA{};classX{virtualshared_ptrfoo();};classY:publicX{virtualshared_ptrfoo();};返回类型不是协变的(因此它们也不是合法的),但如果我使用原始指针代替它们就会是。解决此问题的常用习语是什么(如果有的话)? 最佳答案 我认为解决方案基本上是不可能的,因为协方差取决于与智能指针不兼容的指针算法。当Y::foo返回shared_ptr对于动态调用者,它必须转换为shared_ptr使用前

java - 在像 Stream.reduce() 这样的 API 中选择不变性的充分理由是什么?

审查Java8StreamAPI设计,我对Stream.reduce()上的通用不变性感到惊讶论据:Ureduce(Uidentity,BiFunctionaccumulator,BinaryOperatorcombiner)同一个API的一个看似更通用的版本可能对U的各个引用应用了协变/逆变。,如:Ureduce(Uidentity,BiFunctionaccumulator,BiFunctioncombiner)这将允许以下情况,目前这是不可能的://Assumingwewanttoreusethesetoolsallovertheplace:BiFunctionnumberAdd

java - 在像 Stream.reduce() 这样的 API 中选择不变性的充分理由是什么?

审查Java8StreamAPI设计,我对Stream.reduce()上的通用不变性感到惊讶论据:Ureduce(Uidentity,BiFunctionaccumulator,BinaryOperatorcombiner)同一个API的一个看似更通用的版本可能对U的各个引用应用了协变/逆变。,如:Ureduce(Uidentity,BiFunctionaccumulator,BiFunctioncombiner)这将允许以下情况,目前这是不可能的://Assumingwewanttoreusethesetoolsallovertheplace:BiFunctionnumberAdd

java泛型协方差

我无法理解以下文章:http://www.ibm.com/developerworks/java/library/j-jtp01255.html下,Genericsarenotcovariant作者说,BecauselnisaList,addingaFloattoitseemsperfectlylegal.Butiflnwerealiasedwithli,thenitwouldbreakthetype-safetypromiseimplicitinthedefinitionofli--thatitisalistofintegers,whichiswhygenerictypescanno

java泛型协方差

我无法理解以下文章:http://www.ibm.com/developerworks/java/library/j-jtp01255.html下,Genericsarenotcovariant作者说,BecauselnisaList,addingaFloattoitseemsperfectlylegal.Butiflnwerealiasedwithli,thenitwouldbreakthetype-safetypromiseimplicitinthedefinitionofli--thatitisalistofintegers,whichiswhygenerictypescanno

java - Java中的协方差和重载

classA{booleanf(Aa){returntrue;}}classBextendsA{booleanf(Aa){returnfalse;}//overrideA.f(A)booleanf(Bb){returntrue;}//overloadA.f}voidf(){Aa=newA();Aab=newB();Bb=newB();ab.f(a);ab.f(ab);ab.f(b);//(1)false,false,*false*b.f(a);b.f(ab);b.f(b);//(2)false,false,true}你能解释一下第一行最后一个假输出,为什么不是真的吗?

java - Java中的协方差和重载

classA{booleanf(Aa){returntrue;}}classBextendsA{booleanf(Aa){returnfalse;}//overrideA.f(A)booleanf(Bb){returntrue;}//overloadA.f}voidf(){Aa=newA();Aab=newB();Bb=newB();ab.f(a);ab.f(ab);ab.f(b);//(1)false,false,*false*b.f(a);b.f(ab);b.f(b);//(2)false,false,true}你能解释一下第一行最后一个假输出,为什么不是真的吗?

c++ - 将 vector<Derived*> 放入需要 vector<Base*> 的函数中

考虑这些类。classBase{...};classDerived:publicBase{...};这个函数voidBaseFoo(std::vectorvec){...}最后是我的vectorstd::vectorderived;我想通过derived发挥作用BaseFoo,但编译器不让我。我如何解决这个问题,而不将整个vector复制到std::vector? 最佳答案 vector和vector是不相关的类型,所以你不能这样做。这在C++FAQhere中有解释。.您需要将变量从vector更改为到vector并插入Derive

c++ - 将 vector<Derived*> 放入需要 vector<Base*> 的函数中

考虑这些类。classBase{...};classDerived:publicBase{...};这个函数voidBaseFoo(std::vectorvec){...}最后是我的vectorstd::vectorderived;我想通过derived发挥作用BaseFoo,但编译器不让我。我如何解决这个问题,而不将整个vector复制到std::vector? 最佳答案 vector和vector是不相关的类型,所以你不能这样做。这在C++FAQhere中有解释。.您需要将变量从vector更改为到vector并插入Derive