Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why std::atomic is not trivial type in only Visual C++?

The Folly library requires that std::atomic<hazptr_obj*> should be a trivial type. This works with gcc and clang, but failing for Visual C++ even for std::atomic<int>. Why does std::is_trivial return false?

#include <type_traits>
#include <atomic>

static_assert(
    std::is_trivial<std::atomic<int>>::value,
    "std::atomic<int> not trivial");
like image 709
KindDragon Avatar asked Feb 14 '18 18:02

KindDragon


People also ask

What is std:: atomic?

Each instantiation and full specialization of the std::atomic template defines an atomic type. Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.

What is trivially copyable C++?

A trivially copyable class is a class that: has no non-trivial copy constructors, has no non-trivial move constructors, has no non-trivial copy assignment operators, has no non-trivial move assignment operators, and has a trivial destructor. Copy/move constructors in [class.copy]/12.


1 Answers

std::atomic used to be trivial (which requires Trivially Copyable), but isn't anymore. See this answer for a great and detailed explanation for how and why that changed.

This makes VC compliant and gcc and clang non-compliant at least in C++17. As this was considered a defect by the committee, VC shows the desired behavior for C++11 and C++14, too.

For future reference, the relevant defect is DR #1734, you can see the implementation status for clang here. I'm not aware of an equivalent status page for gcc.

like image 80
Baum mit Augen Avatar answered Oct 26 '22 21:10

Baum mit Augen