error C2280: attempting to reference a deleted function (atomic<int>)

别等时光非礼了梦想. 提交于 2019-12-21 04:46:11

问题


I have a class A with a member variable _atomicVar of type std::atomic<int>.

#include <atomic>

class A
{
public:
    A();
    ~A();

private:
    std::atomic<int> _atomicVar;
};

If I build the project I get the following error:

error C2280: 'std::atomic<int>::atomic(const std::atomic<int> &)' : attempting to reference a deleted function

I'm mainly a C# developer so I don't know every detail of C++ (yet). I don't know where I use the copy c'tor of atomic<int>.
I also tried to initialize _atomicVar:

std::atomic<int> _atomicVar { 0 };

... but that didn't work.
I would expect that _atomicVar (without an explicit initialization) would get initialized with the default value for int.
Can you tell me why this error occurs?


回答1:


That's because copy constructor of std::atomic is deleted.

See this documentation page.

Since you do not define explicit copy constructor for A, compiler generates default one, which simply calls copy constructors for all members (which is not allowed for std::atomic).

Solution:

class A
{
public:
    A();
    A(const A& origin); // add this line
    ~A();
private:
    std::atomic<int> _atomicVar;
};

A::A(const A& origin)
: _atomicVar(0) //zero-initialize _atomicVar
{
}

EDIT

If you wonder, why atomic types are not copyable, you may want to read this question, especially accepted answer. If you want to copy value of std::atomic, you can do it:

A::A(const A& origin)
: _atomicVar(origin._atomicVar.load())
{
}

But keep in mind, that this operation itself will not be an atomic one (and, for most logics, meaningless).

Also, you may also want to define explicit assignment operator (remember about Rule of Three).

The best option for proper behaviour of your program would be deleting these two methods:

class A
{
public:
    A();
    A(const A&) = delete;
    ~A();

    A& operator=(const A&) = delete;

private:
    std::atomic<int> _atomicVar;
};

If your compiler doesn't support this (e.g. any VC before VC12), declare them as private and do not provide a body:

class A
{
public:
    A();
    ~A();

private:
    //do not define these two
    A(const A&);
    A& operator=(const A&);

private:
    std::atomic<int> _atomicVar;
};


来源:https://stackoverflow.com/questions/29332897/error-c2280-attempting-to-reference-a-deleted-function-atomicint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!