C++ Logo

std-discussion

Advanced search

Re: Should std::atomic<T*> have operator->?

From: Jeremy Ong <jeremycong_at_[hidden]>
Date: Mon, 21 Oct 2019 01:44:24 -0600
I imagine it discourages pathologically poor performing code as in the
following:

// atomic<Foo*> foo;
foo->do_one_thing();
foo->do_another_thing();
foo->do_a_final_thing();

You have now done an atomic load 3 times and it is possible that stores or
loads to foo have occurred in between. The pattern for atomic usage is to
typically load once, perform a change, and possibly compare/swap in a loop
in a manner that prevents ABA bugs.

On Mon, Oct 21, 2019 at 1:38 AM Giuseppe D'Angelo via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Hello,
>
> I've been wondering why std::atomic<T*> has an implicit operator T*()
> (that simply does a seq_cst load), but not an operator->. I'm not a big
> fan of having the implicit conversion operator in the first place, but
> since it's there, why not also providing operator* / operator->? Is it
> by design?
>
>
> In other words, if this code is "reasonable":
>
> > std::atomic<S *> a;
> > S *s = a;
> > s->f();
>
> then why shouldn't one also be allowed to simply write this:
>
> > std::atomic<S *> a;
> > a->f();
>
> If, on the other hand, one is not supposed to use the conversion
> operator: should it be deprecated / turned into explicit?
>
> (No, I'm not even remotely proposing to change the rules for operator->
> expressions to consider implicit conversions for the lhs.)
>
> Thanks for reading,
> --
> Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
> KDAB (France) S.A.S., a KDAB Group company
> Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
> KDAB - The Qt, C++ and OpenGL Experts
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2019-10-21 02:46:50