As mentioned in the linked thread, std::atomic<T>
requires T
to be trivially copyable.
Here T
is std::optional<U>
for some U
. The std::optional
class template forwards triviality: its copy constructor, move constructor, copy assignment operator, move assignment operator, and destructor are each trivial if the same is trivial for U
.
There is no ironclad guarantee that std::chrono::time_point<std::chrono::system_clock>
is trivially copyable. The underlying representation of the time_point
may be an arithmetic type or "a class emulating an arithmetic type" ([time.duration]/2), which I read as being sufficiently flexible to permit e.g. nontrivial copy constructors (even if they have the same semantics as a trivial copy constructor). If that's the case, the time_point
will not be trivially copyable.
In any case, std::atomic<std::optional<std::chrono::time_point<std::chrono::system_clock>>>
is safe to use if it compiles. The compiler must issue a diagnostic if the T
in std::atomic<T>
is not trivially copyable, since the standard says "The program is ill-formed" if this condition is not met ([atomics.types.generic]/1). In previous versions of the standard, the wording was "... shall be trivially copyable", which is also a diagnosable rule since it doesn't say "otherwise the behavior is undefined" or anything similar.