C++ Logo


Advanced search

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

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Mon, 21 Oct 2019 09:38:24 +0200

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

Received on 2019-10-21 02:40:45