covariance

When is C++ covariance the best solution?

邮差的信 提交于 2019-11-27 03:03:36
问题 This question was asked here a few hours ago and made me realise that I have never actually used covariant return types in my own code. For those not sure what covariance is, it's allowing the return type of (typically) virtual functions to differ provided the types are part of the same inheritance hierarchy. For example: struct A { virtual ~A(); virtual A * f(); ... }; struct B : public A { virtual B * f(); ... }; The different return types of the two f() functions are said to be covariant.

C# Covariance on subclass return types

蓝咒 提交于 2019-11-27 02:06:20
Does anyone know why covariant return types are not supported in C#? Even when attempting to use an interface, the compiler complains that it is not allowed. See the following example. class Order { private Guid? _id; private String _productName; private double _price; protected Order(Guid? id, String productName, double price) { _id = id; _productName = productName; _price = price; } protected class Builder : IBuilder<Order> { public Guid? Id { get; set; } public String ProductName { get; set; } public double Price { get; set; } public virtual Order Build() { if(Id == null || ProductName ==

Why covariance does not work with generic method

情到浓时终转凉″ 提交于 2019-11-27 02:06:19
问题 Assume I have interface and class: public interface ITree {} public class Tree : ITree {} As IEnumerable<T> is covariant , the code line below is compiled successfully: IEnumerable<ITree> trees = new List<Tree>(); But when I put it into generic method: public void Do<T>() where T : ITree { IEnumerable<ITree> trees = new List<T>(); } I get compiled error from compiler: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.IEnumerable'. An

Getting a vector<Derived*> into a function that expects a vector<Base*>

你。 提交于 2019-11-27 01:49:09
Consider these classes. class Base { ... }; class Derived : public Base { ... }; this function void BaseFoo( std::vector<Base*>vec ) { ... } And finally my vector std::vector<Derived*>derived; I want to pass derived to function BaseFoo , but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector<Base*> ? ChrisN vector<Base*> and vector<Derived*> are unrelated types, so you can't do this. This is explained in the C++ FAQ here . You need to change your variable from a vector<Derived*> to a vector<Base*> and insert Derived objects into it. Also, to

T must be contravariantly valid

本秂侑毒 提交于 2019-11-27 01:31:42
What is wrong with this? interface IRepository<out T> where T : IBusinessEntity { IQueryable<T> GetAll(); void Save(T t); void Delete(T t); } It says: Invalid variance: The type parameter 'T' must be contravariantly valid on 'MyNamespace.IRepository.Delete(T)'. 'T' is covariant. Consider what would happen if the compiler allowed that: interface IR<out T> { void D(T t); } class C : IR<Mammal> { public void D(Mammal m) { m.GrowHair(); } } ... IR<Animal> x = new C(); // legal because T is covariant and Mammal is convertible to Animal x.D(new Fish()); // legal because IR<Animal>.D takes an Animal

How can I use covariant return types with smart pointers?

一曲冷凌霜 提交于 2019-11-27 01:23:08
I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual studio it raises C2555: overriding virtual function return type differs and is not covariant If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to covariant return types in C++). I can see the problem is because boost::shared_ptr of Ret1 is

ref and out parameters in C# and cannot be marked as variant

此生再无相见时 提交于 2019-11-27 01:13:14
What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the following. public delegate R Reader<out R, in A>(A arg, string s); public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader) { pReader = ReadFromPeon; } static object ReadFromPeon(Peon p, string propertyName) { return p.GetType().GetField(propertyName).GetValue(p); }

Generic Variance in C# 4.0

两盒软妹~` 提交于 2019-11-27 01:08:34
问题 Generic Variance in C# 4.0 has been implemented in such a way that it's possible to write the following without an exception (which is what would happen in C# 3.0): List<int> intList = new List<int>(); List<object> objectList = intList; [Example non-functional: See Jon Skeet's answer] I recently attended a conference where Jon Skeet gave an excellent overview of Generic Variance, but I'm not sure I'm completely getting it - I understand the significance of the in and out key words when it

Co- and Contravariance bugs in .NET 4.0

廉价感情. 提交于 2019-11-27 00:52:17
问题 Some strange behavior with the C# 4.0 co- and contravariance support: using System; class Program { static void Foo(object x) { } static void Main() { Action<string> action = _ => { }; // C# 3.5 supports static co- and contravariant method groups // conversions to delegates types, so this is perfectly legal: action += Foo; // since C# 4.0 much better supports co- and contravariance // for interfaces and delegates, this is should be legal too: action += new Action<object>(Foo); } } It's

Covariance and contravariance in programming languages

被刻印的时光 ゝ 提交于 2019-11-27 00:30:47
问题 Can anyone explain me, the concept of covariance and contravariance in programming languages theory? 回答1: Covariance is pretty simple and best thought of from the perspective of some collection class List . We can parameterize the List class with some type parameter T . That is, our list contains elements of type T for some T . List would be covariant if S is a subtype of T iff List[S] is a subtype of List[T] (Where I'm using the mathematical definition iff to mean if and only if .) That is,