GCC 4.8 模板类默认合成构造函数的坑

测 CentOS 7 自带的 GCC 4.8.5,但是去 goldbot 上测了下发现 4.9.0 也有问题,直到 4.9.1 才修复。

最简单的复现代码:

#include <atomic>
template <typename T>
struct Demo {
    std::atomic_bool flag{false};
    T value;
int main() {
    Demo<int> demo;
    return 0;

GCC 4.9.0 编译报错:

1.cc: In constructor 'Demo<int>::Demo()':
1.cc:4:8: error: use of deleted function 'std::atomic_bool::atomic_bool(const std::atomic_bool&)'
 struct Demo {
In file included from 1.cc:1:0:
/usr/include/c++/4.8.2/atomic:62:5: error: declared here
     atomic_bool(const atomic_bool&) = delete;
1.cc: In function 'int main()':
1.cc:10:15: note: synthesized method 'Demo<int>::Demo()' first required here 
     Demo<int> demo;
               ^

去掉模板后编译通过。

#include <atomic>
using T = int;
struct Demo {
    std::atomic_bool flag{false};
    T value;
int main() {
    Demo demo;
    return 0;

当然,基于模板类最简单的修复方式就是,手动加个构造函数:

#include <atomic>
template <typename T>
struct Demo {
    Demo() {}
    std::atomic_bool flag{false};
    T value;