如果定义了 std::atomic_flag 类型变量,则初始化必须在定义的时候初始化,不能在构造函数中初始化。
如果在构造函数中初始化,则会出现出现下面两种错误
class A { private: std::atomic_flag flag; public: A() { flag = ATOMIC_FLAG_INIT; } }
错误:
error C2280: 'std::atomic_flag &std::atomic_flag::operator =(const std::atomic_flag &)': attempting to reference a deleted function
class A { private: std::atomic_flag flag; public: A() : flag(ATOMIC_FLAG_INIT) { } }
错误
error C2280: 'std::atomic_flag::atomic_flag(const std::atomic_flag &)': attempting to reference a deleted function
正确的初始化方式是:
class A { private: std::atomic_flag flag = ATOMIC_FLAG_INIT; public: A() { } }