Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is load() called implicitly when using a method from an std::atomic?

I've noticed that, at least on the surface, the last lines of code seem to be equivalent:

std::atomic<int*> a;
a.store(new int{11});
std::cout << *a.load() << "\n";
std::cout << *a << "\n";

It seems that the value stored inside an atomic wrapper over a pointer can be accessed by calling the dereference operator directly or by loading the pointer and then calling the operator.

Is there a difference between the two ? Is the usage of the dereference operator implicitly calling load() ?

like image 229
George Avatar asked Apr 09 '26 00:04

George


1 Answers

a has operator int*() called on it, and then the indirection operator is applied to the pointer. The conversion function is the same as calling load, so they're nearly identical.

The only difference would be if the type was user defined because it would consume your one user-defined implicit conversion.

like image 145
Weak to Enuma Elish Avatar answered Apr 11 '26 15:04

Weak to Enuma Elish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!