Topic: shared_ptr inner workings. Are conversions customizable?


Author: german diago <germandiago@gmail.com>
Date: Sun, 6 Feb 2011 19:09:55 CST
Raw View
Hi all. I'm trying to implement a runtime concept framework in C++
(www2.research.att.com/~bs/oops08.pdf).
I have some classes that represent concepts at runtime:

class MyConceptClass;

class MyRefinedConceptClass;


I have a conversion operator from MyRefinedConceptClass to
MyConceptClass. So this works:


MyRefinedConceptClass my_object;

MyConceptClass & ref = my_object;


What it's not working is the same thing but with shared_ptr:

shared_ptr<MyRefinedConceptClass> my_object_ptr;

shared_ptr<MyConceptClass> ptr = my_object_ptr;

Is there a way to make such code work. Remember I can't (at least I
couldn't find a way to do it) make
MyRefinedConceptClass to inherit from MyConceptClass, since this way I
would duplicate storage for
the held type. I tried with metaprogramming and SFINAE in template
parameters for the class, but this
generates different classes from the template parameters. Is this
solvable in any way? Thank you very much.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Wed, 9 Feb 2011 01:59:13 CST
Raw View
[To the mods: c.s.c++ seems not to respond since more than 24 hours,
therefore
I send this message directly to you]

Am 07.02.2011 02:09, schrieb german diago:
>
> Hi all. I'm trying to implement a runtime concept framework in C++
> (www2.research.att.com/~bs/oops08.pdf).
> I have some classes that represent concepts at runtime:
>
> class MyConceptClass;
>
> class MyRefinedConceptClass;
>
> I have a conversion operator from MyRefinedConceptClass to
> MyConceptClass.

I assume that you mean a conversion function here, because you cannot
conversion operators (static_cast, reinterpret_cast, etc.) are not
customization points, so you cannot replace them or overload them.

OK, so you provided functionality that allows to convert a
MyRefinedConceptClass object to an MyConceptClass object.

What object or reference does your conversion function return?

> So this works:
>
> MyRefinedConceptClass my_object;
>
> MyConceptClass&  ref = my_object;

If this "works", your conversion function needs to return an lvalue -
where does this come from?

> What it's not working is the same thing but with shared_ptr:
>
> shared_ptr<MyRefinedConceptClass>  my_object_ptr;
>
> shared_ptr<MyConceptClass>  ptr = my_object_ptr;

This is ill-formed, because MyRefinedConceptClass* is not convertible
to MyConceptClass*. So, the type-relations are different from yours -
even though I don't understand why there would be a difference to
ensure that MyRefinedConceptClass derives from MyConceptClass, since
your conversion function has to return an lvalue of MyConceptClass
anyway.

> Is there a way to make such code work. Remember I can't (at least I
> couldn't find a way to do it) make
> MyRefinedConceptClass to inherit from MyConceptClass, since this way I
> would duplicate storage for
> the held type.

The paper you mention uses inheritance to 'model' refinement relations
- have you got performance data that demonstrates that the inheritance
approach doesn't perform well enough?

> I tried with metaprogramming and SFINAE in template
> parameters for the class, but this
> generates different classes from the template parameters. Is this
> solvable in any way? Thank you very much.

It seems that either std::shared_ptr is not the right thing for you
(you may need to create your own smart pointer that behaves like a
smart reference, which is not possible in perfect manner in C++), or
you need to change your convertibility requirements.

HTH & Greetings from Bremen,

Daniel Kr=FCgler


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: paul_71 <paul.michalik@gmx.net>
Date: Thu, 24 Feb 2011 07:21:07 CST
Raw View
 Hi,

shared_ptr<MyRefinedConceptClass> my_object_ptr;
shared_ptr<MyConceptClass> ptr = my_object_ptr;

the above expressions are well formed, iff MyRefinedConceptClas* is
convertible to MyConceptClass* (note the pointers). Since you've
apparently managed to accomplish this for references (how?) you could
do the same for pointers. However I doubt this is cleanly possible
without inheritance....


Cheers,

Paul


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]