Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quick Intro question to smart pointers.

53 views
Skip to first unread message

Doug Mika

unread,
Aug 12, 2015, 10:19:05 PM8/12/15
to
I have the following code snippet:

std::shared_ptr<my_data> p;
void process_global_data()
{
std::shared_ptr<my_data> local=std::atomic_load(&p);
process_data(local);
}

what confuses me is why we don't write

std::shared_ptr<my_data> local=std::atomic_load(p);

after all, p is already a pointer, won't getting an address of p (&p) lead to me having a pointer to a pointer to my_data?

Thanks

Alf P. Steinbach

unread,
Aug 13, 2015, 3:17:17 AM8/13/15
to
It's just the design of `atomic_load` is imperfect, that it expects a
pointer rather than a reference as argument.

You can look up standard library functions in any decent reference, e.g.
<url: http://en.cppreference.com/w/cpp/atomic/atomic_load>.

That said, the code shown appears to be very meaningless, and exhibiting
several Unholy Worst Practices™. So whatever source you got it from is
not a good source of examples to learn from.


Cheers & hth.,

- Alf

--
Using Thunderbird as Usenet client, Eternal September as NNTP server.

Bo Persson

unread,
Aug 13, 2015, 11:33:50 AM8/13/15
to
On 2015-08-13 09:16, Alf P. Steinbach wrote:
> On 13-Aug-15 4:18 AM, Doug Mika wrote:
>> I have the following code snippet:
>>
>> std::shared_ptr<my_data> p;
>> void process_global_data()
>> {
>> std::shared_ptr<my_data> local=std::atomic_load(&p);
>> process_data(local);
>> }
>>
>> what confuses me is why we don't write
>>
>> std::shared_ptr<my_data> local=std::atomic_load(p);
>>
>> after all, p is already a pointer, won't getting an address of p (&p)
>> lead to me having a pointer to a pointer to my_data?
>
> It's just the design of `atomic_load` is imperfect, that it expects a
> pointer rather than a reference as argument.

The interface was supposed to be compatible with C, and a pointer is all
they have.


Bo Persson


Alf P. Steinbach

unread,
Aug 13, 2015, 5:00:59 PM8/13/15
to
* 13-Aug-15 5:33 PM, Bo Persson:
> * 2015-08-13 09:16, Alf P. Steinbach:
> >
> > It's just the design of `atomic_load` is imperfect, that it expects a
> > pointer rather than a reference as argument.
>
> The interface was supposed to be compatible with C, and a pointer is all
> they have.

Well, 2 things wrong with that idea:

* The function is in namespace `std`, no such in C.

* There's no problem with the same function having binding as foo(T&) in
C++ and as foo(T*) in C; that's what language bindings are all about,
offering an interface that's suitable for the language.
0 new messages