covariance

How to accomplish covariant return types when returning a shared_ptr?

喜夏-厌秋 提交于 2019-11-30 08:13:18
using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around this, if there is one? I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers. When Y::foo returns shared_ptr<B> to a dynamic caller, it must be cast to shared_ptr<A> before use. In your

Selectively disable subsumption in Scala? (correctly type List.contains)

最后都变了- 提交于 2019-11-30 07:21:28
问题 List("a").contains(5) Because an Int can never be contained in a list of String , this should generate an error at compile-time , but it does not. It wastefully and silently tests every String contained in the list for equality to 5 , which can never be true ( "5" never equals 5 in Scala). This has been named "the 'contains' problem". And some have implied that if a type system cannot correctly type such semantics, then why go through the extra effort for enforcing types. So I consider it is

C++: How can I avoid “invalid covariant return type” in inherited classes without casting?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 06:51:32
I have a quite complex class hierarchy in which the classes are cross-like depending on each other: There are two abstract classes A and C containing a method that returns an instance of C and A, respectively. In their inherited classes I want to use a co-variant type, which is in this case a problem since I don't know a way to forward-declare the inheritance relation ship. I obtain a "test.cpp:22: error: invalid covariant return type for ‘virtual D* B::outC()’"-error since the compiler does not know that D is a subclass of C. class C; class A { public: virtual C* outC() = 0; }; class C {

DELPHI: Generics and polymorphism

只谈情不闲聊 提交于 2019-11-30 05:11:24
This has been asked several different ways already - but I haven't found my answer yet. Can someone clarify a few things for me please. Using : Delphi XE2 I have quite a big BaseObject that I use for almost everything. Along with it I have a Generic list - BaseList. Delarations go like this : TBaseObject = class ... a lot of properties and methods ... end; TBaseList<T: TBaseObject> = class(TObjectList<T>) ... some properties and methods ... end; I have recently tried to change the TBaseList declaration from a very old TStringList using Objects[] property... to this never more versatile

Interfaces and covariance problem

╄→гoц情女王★ 提交于 2019-11-30 04:49:30
问题 I have a particular class that stores a piece of data, which implements an interface: template<typename T> class MyContainer : public Container<T> { class Something : public IInterface { public: // implement *, ->, and ++ here but how? private: T x; }; // implement begin and end here, but how? private: Something* data; // data holds the array of Somethings so that references to them can be returned from begin() and end() to items in this array so the interface will work, but this makes the

Is C# 4.0 Tuple covariant

倖福魔咒の 提交于 2019-11-30 01:54:57
问题 (I would check this out for myself, but I don't have VS2010 (yet)) Say I have 2 base interfaces: IBaseModelInterface IBaseViewInterface And 2 interfaces realizing those: ISubModelInterface : IBaseModelInterface ISubViewInterface : IBaseViewInterface If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface> . In C# 3 I can't do this even though the sub interfaces realize the

C# variance annotation of a type parameter, constrained to be value type

我与影子孤独终老i 提交于 2019-11-30 01:14:55
It is possible in C# to add variance annotation to type parameter, constrained to be value type: interface IFoo<in T> where T : struct { void Boo(T x); } Why is this allowed by compiler if variance annotation makes completely no sense in a such situation? Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It's allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler. Compiler warnings and errors are features , and in order for a feature to be implemented,

Calculating Covariance with Python and Numpy

谁说胖子不能爱 提交于 2019-11-29 23:11:35
I am trying to figure out how to calculate covariance with the Python Numpy function cov. When I pass it two one-dimentional arrays, I get back a 2x2 matrix of results. I don't know what to do with that. I'm not great at statistics, but I believe covariance in such a situation should be a single number. This is what I am looking for. I wrote my own: def cov(a, b): if len(a) != len(b): return a_mean = np.mean(a) b_mean = np.mean(b) sum = 0 for i in range(0, len(a)): sum += ((a[i] - a_mean) * (b[i] - b_mean)) return sum/(len(a)-1) That works, but I figure the Numpy version is much more efficient

Why doesn't C# do “simple” type inference on generics?

心已入冬 提交于 2019-11-29 22:36:51
问题 Just curious: sure, we all know that the general case of type inference for generics is undecidable. And so C# won't do any kind of sub-typing at all: if Foo<T> is generic, Foo<int> isn't a subtype of Foo<T> , or Foo<Object> or of anything else you might cook up. And sure, we all hack around this with ugly interface or abstract class definitions. But... if you can't beat the general problem, why not just limit the solution to cases that are easy. For example, in my list above, it is OBVIOUS

Co-variant array conversion from x to y may cause run-time exception

烂漫一生 提交于 2019-11-29 21:09:11
I have a private readonly list of LinkLabel s ( IList<LinkLabel> ). I later add LinkLabel s to this list and add those labels to a FlowLayoutPanel like follows: foreach(var s in strings) { _list.Add(new LinkLabel{Text=s}); } flPanel.Controls.AddRange(_list.ToArray()); Resharper shows me a warning: Co-variant array conversion from LinkLabel[] to Control[] can cause run-time exception on write operation . Please help me to figure out: What does this means? This is a user control and will not be accessed by multiple objects to setup labels, so keeping code as such will not affect it. What it