f



Covariance and xxx_ptr

{ 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! ]

0
Javier
3/5/2016 7:00:41 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
2244 Views

Similar Articles

[PageSpeed] 45

On Saturday, 5 March 2016 15:10:09 UTC+2, Javier  wrote:
>
> 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.

In some other context also covariance of 'vector<Provider>' and 
'vector<ConcreteProvider>' (or iterators to such) may feel like
intuitively making sense.

Simulation of covariance (where it makes sense) can be made manually
by using private virtual 'doUse()' and public non-virtual 'use()' that calls
'doUse()' and converts its return type. The 3-4 additional lines per class
are not that much if you need it and if you don't need it then YAGNI.   

> 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.

The inbuilt covariance is only for raw pointers and references and there are
no covariance of class type objects (that smart pointers are). Covariance is
about inheritance and not about convertibility but converting objects of
derived class into object of base class means object slicing so it has to be
a pointer or reference.

Smart pointers indeed manage to simulate covariance somewhat with
conversion assignment templates and conversion constructor templates
in order to be more similar to raw pointers.
 
> 
> Are there technical limitations? Seems to me that no abstractions would
> be broken.

Turning all implicit convertibility into covariance is likely bad idea. 
Implicit convertibility often confuses programmers and it does not 
need to have more ways of application. Adding some "magic" 
specially to smart pointers is likely even worse since these would
then be core language features that only look like library classes.   


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

0
ISO
3/7/2016 12:11:55 PM
Reply:

Similar Artilces:

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta > * open source - preferable, but not 100% necessary what's wrong with using C? Friedrich > what's wrong with using C? Too low level, no built-in garbage collection. -- Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: >> what's wrong with using C? > > Too low level, Well the Smalltalk people say, have a small language and a big library. I assume C's library status beyond that of Smalltalk. > no built-in garbage collection. Use the Boehm Weisser GC. Friedrich "Kevin Albrecht" <kevin@albrecht.net> wrote in message news:<2TiYb.237892$rj7.1641209@twister.tampabay.rr.com>... > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) > ...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! Throne Software wrote: > Throne Software has opened up C, C++ and C# Forums at: > > http://www.thronesoftware.com/forum/ For all of your forums newsgroups already exist. > Join us! Why? Don't see any reason to support your marketing. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system. Full of the blind (opinionated) leading the blind. The problem is that as the experienced people who have been around a long while are on usenet. the system works. Why move. Certainly not to a forum on a commercial web site. The web site might go down. Usenet can't. Web based systems are not as safe. I have to trust you about cookies and all sorts. (active X etc). Usenet is international and independent. It is not controlled by anyone. Why would anyone move from this system to a web based one in a a rogue state? There is no point in running a web based forum except as a support forum for your own products. -- \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/ /\/\/ chris@phaedsys.org www.phaedsys.org \/\/\ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Chris Hills wrote: > In article <1120963701.862...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It manages the memory for you, so you don't have to allocate/deallocate all the time (the source of some of the most nasty bugs in C). Java and C# learnt that feature from ST. 2. It has single inheritance. 3. All the development is in a single file, called the image, so you don't have to fish for where did you put that missing class. Yet, you don't have to go through thousands of lines of code to find the method you wrote three months ago. 4. It's the language for which it's easier to work within the Agile methodologies. 5. It's strongly typed without being unnecessarily redundant. 6. It's "wacky" interface releases you from the need to use curlys. 7. It comes with a huge library, so you don't need to reinvent the wheel as much. 8. It's debugger is highly integrated and friendly, so the experience of findin...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

C or C++
I think C++ is better for game programming -- comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry. t3chn0n3rd wrote: > > I think C++ is better for game programming As the official authority for such things in clcm, I hereby give you permission to go elsewhere and use C++ for your game programming. -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www...

More about C and C++
Hello... I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Josep...

More about C and C++
Hello, I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Joseph ...

c to c++
hi, i was wondering whether its worth converting from c to c++? i'm lazy but if its worth the effort... ______________________ http://monsterbeans.com/ | --------------------------------------- eminhanif@googlemail.com wrote: > hi, > i was wondering whether its worth converting from c to c++? > i'm lazy but if its worth the effort... Rather than participating in the language war flamefest your message invites, I will simply make the most obvious point: it is worth your while to learn any language that will be used in connection with your work. This includes learning a ...

c++ or c#
Hi. I'm new to c++ and still learning it. I've been advised that c and c++ are now becoming obsolete..I better learn c# (supposedly the best language available) ..is it true?? * sandy: > Hi. I'm new to c++ and still learning it. I've been advised that c > and c++ are now becoming obsolete..I better learn c# (supposedly the > best language available) ..is it true?? The premise is false but many, including me, agree with the conclusion that you should learn e.g. C# first (obviously for different reasons). Learning C# or another language first might take you months o...

lang.c++
Dear Friend, I have found some great articles at: http://www.familyandtwist.com/ http:www.articlestwist.com/ http://www.downloadstwist.com/ http://www.wallpaperstwist.com/ Read them all. Thanks ...

[Lang C]Linear Algebra, Calculus In language C with gnuplot.
hello, I present my work : a freeware, (Language C, GnuPlot). Linear algebra : (fractions, 30 packages). http://www.geocities.com/xhungab/package.html The purpose of this work is to verify with numeric applications, some properties of the linear algebra. addm, subm, multm, powm, smultm, transpose, det, minor, mminor, cofactor, mcofactor, adjoint, inverse, gauss, gaussjordan, LU, norm,distance,innerproduct,coldim,rowdim, rank,nullity, Inner productin Mnxn, inner product on R**n generated by A, Weighed Euclidean Inner Produc, Eigenvalue, Eigenvector, Cayley-Hamilton theorem, similarity. ...

Lang C
www.BrandonsMansion.com ...

lang.c
Dear Friend, I have found some great articles at: http://www.familyandtwist.com/ http:www.articlestwist.com/ http://www.downloadstwist.com/ http://www.wallpaperstwist.com/ Read them all. Thanks <mahmar06@gmail.com> ha scritto nel messaggio news:1182236687.057273.175370@a26g2000pre.googlegroups.com... > Dear Friend, > > I have found some great articles at: > > http://www.familyandtwist.com/ > http:www.articlestwist.com/ > http://www.downloadstwist.com/ > http://www.wallpaperstwist.com/ > > Read them all. I would bother to read them if you told us wha...

More about C and C++
Hello, I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Josep...

c++ or c#
hello all experiancesd techies, i am a final yr engg student with knowledge of c++ n java. i want to start windows programing, n hence have to decide whether to choose my language as c++ or c#. please guide me which language would benefit in the long run, if i intend to persue a job in a product development company/( like adobe) after graduation,(not in a consultancy like acenture). also suggest me good books for that. thanx a lot <pravin.chaudhary@gmail.com> wrote in message news:1131971088.291580.42500@g47g2000cwa.googlegroups.com... > i am a final yr engg student with ...

prob w/the C prog. lang. book(ANSI C)
I just bought The C Programming Language, by the guy who made C and so...I have 2 different compilers, free ones, and I don't know if I typed the code in wrong or if it's a problem with my compilers, do any of you know? They were free compilers: Icc-win32 and Bloodshed Dev C++(uses C too). Are there and compilers that are free besides mars, and gcc-whatever, can't get them to work. Thanks. heres the code, is there anything wrong with it: #include <stdio.h> main() { int fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr ...

Web resources about - Covariance and xxx_ptr - comp.lang.c++.moderated

Covariance - Wikipedia, the free encyclopedia
In probability theory and statistics , covariance is a measure of how much two random variables change together. If the greater values of one ...

Leithner & Co Pty Ltd - Value Investing, Risk and Risk Management - Part I
The bona fide investor does not lose money merely because the market price of his holdings declines, and the fact that a decline may occur does ...

Professor Mark Blows - The School of Biological Sciences Academic Staff - The University of Queensland ...
Access information about the The School of Biological Sciences Academic Staff, University of Queensland

Department of Psychology - McGill University - Montreal Quebec Canada
The following sections give information on the general research areas represented in the Department, with a list of staff most involved in each ...

Risk And Return
One of the most fundamental concepts in finance is that risk and return are correlated. We touched on this a tiny bit in one of the early MBA ...

Interesting links #1
... waiting to see how the Chrome OS will look like, even if I am not a Linux supporter. Inside C# 4.0: dynamic typing, optional parameters, covariance ...

Derivatives Strategy - April'97: The Jorion-Taleb Debate
The Jorion-Taleb Debate The Q&A in our December/January issue, "The WorldAccording to Nassim Taleb," contained a carefully reasoned attack onValue-at-Risk ...

Varia Emoticon
Case 1:12-cv-01899-PGG Document 1 Filed 03/15/12 Page 1 of 26Case 1:12-cv-01899-PGG Document 1 Filed 03/15/12 Page 2 of 26Case 1:12-cv-01899-PGG ...

Highlights of significant results on Orally administered Optina for Diabetic Macular Edema (DME), including ...
Ampio Pharmaceuticals, Inc. (Nasdaq AMPE), a biopharmaceutical company developing innovative proprietary drugs for inflammation, eye disease, ...

Insight
To see a World in a Grain of Sand And a heaven in a Wild Flower Hold Infinity in the palm of your hand And Eternity in an hour. [Blake, ~1803] ...

Resources last updated: 3/5/2016 8:20:22 PM