Boost logo

Boost :

Subject: Re: [boost] Pimpl Again?
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2016-05-31 19:44:36


On Tue, May 31, 2016 at 4:21 PM, Vladimir Batov <
Vladimir.Batov_at_[hidden]> wrote:

> On 2016-06-01 07:10, Emil Dotchevski wrote:
>
>> ...
>> header:
>>
>> struct foo { int critical_; };
>> shared_ptr<foo> create_foo();
>> inline void use_foo_critical( foo * p ) { ++p->critical_; }
>> void use_foo_not_so_critical( foo * );
>>
>> cpp:
>>
>> namespace
>> {
>> struct foo_: foo
>> {
>> int not_so_critiral_;
>> };
>> }
>>
>> shared_ptr<foo> create_foo()
>> {
>> return shared_ptr<foo>(new foo_);
>> }
>>
>> void use_foo_not_so_critical( foo * p )
>> {
>> foo_ * q=static_cast<foo_ *>(p);
>> ++q->not_so_critical_;
>> }
>> ...
>>
>
> Hmm, we probably need to adjust your example (admittedly written in a
> hurry) -- "foo" needs to be virtual (given we wanted efficiency),

You mean polymorphic? It could be.

> static_cast kills all C++ advances in type-safety...

The use of static_cast in this case is perfectly safe, since there is no
way to get to undefined behavior using the public interface only. When I
say "there is no way" I mean that static type checking isn't butchered at
all.

> So, probably it should better be
>
> struct foo_;
> struct foo { int critical_; shared_ptr<foo_>; };
> shared_ptr<foo> create_foo();
>

I disagree, I don't see the benefit of exposing foo_ in the header.

Emil


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk