Groups | Search | Server Info | Login | Register


Groups > comp.lang.c++.moderated > #7395

Covariance and xxx_ptr

Message-ID <47faaec3-f2bb-4f83-a62d-caecab09469e@googlegroups.com> (permalink)
Newsgroups comp.lang.c++.moderated
From Javier <iphone.javier.estrada@googlemail.com>
Subject Covariance and xxx_ptr
Organization unknown
Date 2016-03-05 07:00 -0600

Show all headers | View raw


{ edited to shorten lines to ~70 characters (except code). -mod }

Consider:

struct Consumer {
  virtual Provider* use();
};

struct ConcreteConsumer {
  ConcreteProvider* use() override {...}
};

struct Provider {...};
struct ConcreteProvider : public Provider {...}

The covariant return type is just fine, thank you very much. However, if
I want to create something like:

struct Consumer {
  virtual unique_ptr<Provider> use();
};

struct ConcreteConsumer {
  unique_ptr<ConcreteProvider> use() override {...}
};

struct Provider {...};
struct ConcreteProvider : public Provider {...}

Cannot be done.  I understand the reason, but it seems to me unintuitive
that covariance does not "port" when using any of the std::xxx_ptr
types.
Furthermore, a corresponding implementation for ConcreteConsumer would
have to look like this to type derived from Provider:

struct ConcreteConsumer {
 unique_ptr<Provider> use() override { return
unique_ptr<Provider>{make_unique<ConcreteProvider>();}
};


In essence, I think that std::is_convertible<xxx_ptr<Derived>,
xxx_ptr<Base>>should succeed in the same way that
std::is_convertible<Derived*, Base*> succeeds, and overriding a member
function that returns xxx_ptr where the types are covariant should be
allowed.

Are there technical limitations? Seems to me that no abstractions would
be broken.

Regards,
--Javier


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

Back to comp.lang.c++.moderated | Previous | NextNext in thread | Find similar


Thread

Covariance and xxx_ptr Javier <iphone.javier.estrada@googlemail.com> - 2016-03-05 07:00 -0600
  Re: Covariance and xxx_ptr Öö Tiib <ootiib@hot.ee> - 2016-03-07 12:11 -0600

csiph-web