c++atomicautostdatomic

why atomic does not work with auto varaible


I am not sure what is wrong with following statements , its giving me compilation errors. Can we not use "auto" with atomic variables?

#include <iostream>
#include<future>
#include <atomic>
using namespace std;

int main()
{
  atomic<int> value(10);
  auto   NewValue = value;
}

but if I replace "auto" with "int" , it works. Why?

int main()
{
  atomic<int> value(10);
  int NewValue = value;
}

Compilation error with "auto"

 ||=== Build: Debug in Hello (compiler: GNU GCC Compiler) ===|
 F:\3d\C++CodeProject\Hello\main.cpp||In function 'int main()':|
 F:\3d\C++CodeProject\Hello\main.cpp|11|error: use of deleted function 
 'std::atomic<int>::atomic(const std::atomic<int>&)'|
C:\Program Files 
(x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\atomic|612|note: 
declared here|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Solution

  • auto matches the data type on the right-hand side of the assignment. In this statement:

    auto NewValue = value;
    

    value is a std::atomic<int>, so auto will deduce to std::atomic<int>, not to int like you are expecting.

    IOW, this:

    auto NewValue = value;
    

    Is the same as this:

    atomic<int> NewValue = value;
    

    Which is copy initialization using a copy constructor, but std::atomic has a delete'd copy constructor, which is exactly what the error message says:

    use of deleted function 'std::atomic<int>::atomic(const std::atomic<int>&)'

    std::atomic has a conversion operator for T, which is why int NewValue = value; works.


    UPDATE: if you really want to use auto and have it deduce as int, you can explicitly cast the std::atomic to invoke the conversion operator, eg:

    auto NewValue = (int) value;
    

    or

    auto NewValue = static_cast<int>(value);
    

    Otherwise, use the std::atomic::load() method, like @PeterCordes mentioned in a comment, eg:

    auto NewValue = value.load();