1

I thought g++ 10.3 should have supported the C++20 feature of atomic shared_ptr? But I am still getting the following error

#include <atomic>
#include <thread>
#include <memory>
int main() {
 std::atomic<std::shared_ptr<int>> a = std::make_shared<int>(1);
}
In file included from test.cc:1:
/usr/include/c++/10/atomic: In instantiation of ‘struct std::atomic<std::shared_ptr<int> >’:
test.cc:6:37: required from here
/usr/include/c++/10/atomic:195:21: error: static assertion failed: std::atomic requires a trivially copyable type
 195 | static_assert(__is_trivially_copyable(_Tp),
 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
user4581301
34.1k7 gold badges42 silver badges65 bronze badges
asked Jan 24, 2022 at 23:54
2
  • 1
    Huh. Isn't that annoying. Does std::atomic<std::shared_ptr<int>> a {std::make_shared<int>(1)}; work? Commented Jan 25, 2022 at 0:06
  • atomic<int> x = 5; usually fails to compile on GCC/Clang. Only in form atomic<int> x{5}; it compiles. Commented Jan 25, 2022 at 0:28

1 Answer 1

4

The documentation lists it as supported only since GCC 12.1, i.e. the next release, and compiler explorer shows that your code compiles on GCC trunk: https://godbolt.org/z/4ThzMrjM9

https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

answered Jan 25, 2022 at 0:01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.