struct S
{
int x;
int y;
};
std::atomic<S> asd{{1, 2}}; // what should this be? This doesn't work
Edit: Both {{1, 2}}
and ({1, 2})
work in g++, neither work in clang. Is there a workaround for clang?
struct S
{
int x;
int y;
};
std::atomic<S> asd{{1, 2}}; // what should this be? This doesn't work
Edit: Both {{1, 2}}
and ({1, 2})
work in g++, neither work in clang. Is there a workaround for clang?
This is clang bug 18097. Here's a long thread discussing the issue, which seems to be that clang only supports scalar types for T
in atomic<T>
. The C++11 standard clearly states (§29.5/1) that T
can be any trivially copyable type.
Both the usages shown in the question should match this constructor
constexpr atomic(T) noexcept;
The only way I can think of working around this is to default construct atomic<S>
and then use atomic::store
to initialize the object.
std::atomic<S> asd;
asd.store({1,2});
std::atomic<S> asd({1, 2});
std::atomic<S>
has a constructor which takes a value of type S.
The initializer list {1, 2} is implicitly converted to a temporary S because of this constructor.
clang++
either. Both versions however compile on g++
–
Skat std::atomic<S> asd(S{1,2});
. What version of clang are you using, by the way? –
Avoidance © 2022 - 2024 — McMap. All rights reserved.