[PATCH] libstdc++: improve std::atomic compatibility with Clang

Giuseppe D'Angelo giuseppe.dangelo@kdab.com
Sat Sep 21 09:42:37 GMT 2024


Hello,

The attached patch modifies std::atomic's primary template. The goal is 
to improve compatibility with Clang, while also possibly making it more 
complaint with the changes introduced by P0883 / C++20.

Simplifying, std::atomic has a `T t = T()` NDSMI and a defaulted default 
constructor. The crux of the problem is that Clang seems to be stricter 
than GCC when that constructor is considered / instantiated.

Given a non-default constructible type NDC, doing something like

constexpr bool b = std::is_default_constructible_v<std::atomic<NDC>>;

causes a hard error on Clang because it will "see" the call to `NDC()` 
in the NDSMI. The code is instead accepted by GCC. This hard error will 
happen anywhere one "mentions" std::atomic<NDC>'s default constructor, 
for instance in libstdc++'s C++20 std::pair implementation (uses them in 
the explicit(bool) bits). You can play with this here:

https://gcc.godbolt.org/z/xcr4zK8hx

PR116769 argues that Clang's behavior is the correct one here, so this 
patch improves compat with Clang by removing the defaulted default 
constructor.

A related issue is: what's the value of `b` above? std::atomic's default 
constructor is not constrained, so it should be `true`. Right now we're 
reporting `false` instead.


Thoughts?


Thank you,
-- 
Giuseppe D'Angelo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-libstdc-improve-std-atomic-compatibility-with-Clang.patch
Type: text/x-patch
Size: 4571 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20240921/998b8e3f/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4244 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20240921/998b8e3f/attachment-0001.p7s>


More information about the Libstdc++ mailing list