Структура C++ ссылки говорит: http://en.cppreference.com/w/cpp/atomic/atomicстанда :: атомная тривиальный Copyable
std::atomic may be instantiated with any TriviallyCopyable type T
Однако следующий пример не работает под г ++ 6.2.0
#include <atomic>
#include <functional>
struct Test11 {
int x;
};
struct Test12 {
char x;
};
struct Test13 {
long x;
};
struct Test2 {
char x;
int y;
};
struct Test3 {
int y;
long x;
};
template<typename T, typename... ARGS>
void test(ARGS&& ... args) {
static_assert(std::is_trivially_copyable<T>::value);
std::atomic<T> a;
a.store(T{std::forward<ARGS>(args)...});
}
int main() {
test<Test11>(1);
test<Test12>('\1');
test<Test13>(1L);
test<Test2>('\1',2);
test<Test3>(1,2L);
return 0;
}
Compile: g++-6 -std=c++14 -latomic test.cpp
/tmp/cchademz.o: In function
std::atomic<Test3>::store(Test3, std::memory_order)
: test.cpp:(.text._ZNSt6atomicI5Test3E5storeES0_St12memory_order[_ZNSt6atomicI5Test3E5storeES0_St12memory_order]+0x3e): undefined reference to__atomic_store_16
collect2: error: ld returned 1 exit status
g++-6 --version
g++ (Ubuntu 6.2.0-7ubuntu11) 6.2.0 20161018
Особенно я не понимаю, почему Test2
работает, но Test3
нет.
Любые идеи?
EDIT: добавил -latomic флаг и г ++ версии
Вы пытались добавить '-latomic' в конце строки компиляции случайно? Код компилируется на coliru с помощью '-latomic' (g ++ 6.2): http://coliru.stacked-crooked.com/a/fd421bd3d1715897 – Holt
Вместо этого вы можете включить? –
переезд '-латомный' до конца работал. Но почему? – WaeCo