covariance

What are the drawbacks of C++ covariance return types?

▼魔方 西西 提交于 2021-02-20 03:49:56
问题 I have recently had to deal with C++ covariance return types such as the following construct : struct Base { virtual ~Base(); }; struct Derived : public Base {}; struct AbstractFactory { virtual Base *create() = 0; virtual ~AbstractFactory(); }; struct ConcreteFactory : public AbstractFactory { virtual Derived *create() { return new Derived; } }; It allows the client code to treat the Derived object as a Base type or as a Derived type when needed and especially without the use of dynamic_cast

What are the drawbacks of C++ covariance return types?

最后都变了- 提交于 2021-02-20 03:49:43
问题 I have recently had to deal with C++ covariance return types such as the following construct : struct Base { virtual ~Base(); }; struct Derived : public Base {}; struct AbstractFactory { virtual Base *create() = 0; virtual ~AbstractFactory(); }; struct ConcreteFactory : public AbstractFactory { virtual Derived *create() { return new Derived; } }; It allows the client code to treat the Derived object as a Base type or as a Derived type when needed and especially without the use of dynamic_cast

Why do C# out generic type parameters violate covariance?

。_饼干妹妹 提交于 2021-02-18 20:31:50
问题 I'm unclear as to why the following code snippet isn't covarient? public interface IResourceColl<out T> : IEnumerable<T> where T : IResource { int Count { get; } T this[int index] { get; } bool TryGetValue( string SUID, out T obj ); // Error here? } Error 1 Invalid variance: The type parameter 'T' must be invariantly valid on 'IResourceColl.TryGetValue(string, out T)'. 'T' is covariant. My interface only uses the template parameter in output positions. I could easily refactor this code to

Why do C# out generic type parameters violate covariance?

妖精的绣舞 提交于 2021-02-18 20:31:29
问题 I'm unclear as to why the following code snippet isn't covarient? public interface IResourceColl<out T> : IEnumerable<T> where T : IResource { int Count { get; } T this[int index] { get; } bool TryGetValue( string SUID, out T obj ); // Error here? } Error 1 Invalid variance: The type parameter 'T' must be invariantly valid on 'IResourceColl.TryGetValue(string, out T)'. 'T' is covariant. My interface only uses the template parameter in output positions. I could easily refactor this code to

Why are arrays covariant but generics are invariant?

孤人 提交于 2021-02-11 12:04:35
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There

Why are arrays covariant but generics are invariant?

老子叫甜甜 提交于 2021-02-11 12:02:01
问题 From Effective Java by Joshua Bloch, Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant. Covariant simply means if X is subtype of Y then X[] will also be sub type of Y[]. Arrays are covariant As string is subtype of Object So String[] is subtype of Object[] Invariant simply means irrespective of X being subtype of Y or not , List<X> will not be subType of List<Y>. My question is why the decision to make arrays covariant in Java? There

c# enum covariance doesn't work

廉价感情. 提交于 2021-02-08 13:59:16
问题 I need to use enum as covariant type. Let's say i have this code: public enum EnumColor { Blue = 0, Red = 1, } public class Car : IColoredObject<EnumColor> { private EnumColor m_Color; public EnumColor Color { get { return m_Color; } set { m_Color = value; } } public Car() { } } class Program { static void Main() { Car car = new Car(); IndependentClass.DoesItWork( car ); } } and this code: public interface IColoredObject<out EnumColorType> { EnumColorType Color { get; } } public static class

Making double and std::vector<double> covariant

强颜欢笑 提交于 2021-01-29 22:32:45
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Making double and std::vector<double> covariant

泄露秘密 提交于 2021-01-29 22:31:04
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef

Making double and std::vector<double> covariant

独自空忆成欢 提交于 2021-01-29 21:51:30
问题 I am trying to make a wrapper for "any" data type such that they have common interface called IValue so it will be possible to call get() on any concrete Value and return the value of concrete data type. In simplest case I just want to be able to call get() on double and std::vector<double> . In my understanding these data types need to be covariant (which it is not at all in my code). The following is my raw imagination of the code: //template<typename T> class IValue { protected: // typedef