Consider the following program, named atomic_return.cpp
#include <stdio.h>
#include <atomic>
using namespace std;
class Stats
{
atomic_ullong stats[10] = {};
public:
atomic_ullong get_stat(int index) { return stats[index]; }
};
int main()
{
Stats stat_obj;
stat_obj.get_stat(1);
return 0;
}
I get the following error when I compile:
g++ atomic_return.cpp -o atomic_return
atomic_return.cpp: In member function ‘std::atomic_ullong Stats::get_stat(int)’:
atomic_return.cpp:11:59: error: use of deleted function ‘std::atomic<long long unsigned int>::atomic(const std::atomic<long long unsigned int>&)’
11 | atomic_ullong get_stat(int index) { return stats[index]; }
| ^
In file included from atomic_return.cpp:2:
/usr/include/c++/9/atomic:870:7: note: declared here
870 | atomic(const atomic&) = delete;
| ^~~~~~
Why can I not a return an atomic_ullong
? I am not able to make sense of the error.