试图为原子 C++ 引用已删除的函数

attempting to reference a deleted function for atomic c++

我有一段代码用于 Visual Studio 2013。现在我试图在 Visual Studio 2017 中构建相同的代码,它会抱怨。这是我正在尝试执行的代码。

#include <array>
#include <atomic>

int main()
{

    using TrdRobotStateArray = std::array<std::atomic<double>, 6>;
    TrdRobotStateArray mCurrentPose = { 0.3 };
    printf("%0.3f", mCurrentPose[0]);
    return 0;
}

有了这个,我得到了这个错误:

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

这段代码不是我写的,我正在尝试读入原子变量。但是我仍然不太确定错误是怎么回事。关于原子的解释将不胜感激。谢谢!

更新:

以下是此代码附带的所有错误和警告。所以它会在未来帮助其他人。

1>AtomicTest.cpp
1>AtomicTest.cpp(13): error C4839: non-standard use of class 
'std::atomic<double>' as an argument to a variadic function
1>AtomicTest.cpp(13): note: the constructor and destructor will not be 
called; a bitwise copy of the class will be passed as the argument
1>AtomicTest.cpp(11): note: see declaration of 'std::atomic<double>'
1>AtomicTest.cpp(13): error C2280: 'std::atomic<double>::atomic(const 
std::atomic<double> &)': attempting to reference a deleted function
1>C:\Program Files (x86)\Microsoft Visual 
Studio17\Professional\VC\Tools\MSVC.11.25503\include\atomic(689): 
note: see declaration of 'std::atomic<double>::atomic'
1>C:\Program Files (x86)\Microsoft Visual 
Studio17\Professional\VC\Tools\MSVC.11.25503\include\atomic(689): 
note: 'std::atomic<double>::atomic(const std::atomic<double> &)': function 
was explicitly deleted
1>AtomicTest.cpp(13): warning C4477: 'printf' : format string '%0.3f' 
requires an argument of type 'double', but variadic argument 1 has type 
'std::atomic<double>'
1>Done building project "AtomicTest.vcxproj" -- FAILED.

原子变量不是 CopyConstructible

这是C++标准的要求,所以VisualStudio 2017是正确的。

您必须显式加载该值才能对其进行编译:

printf("%0.3d", mCurrentPose[0].load());

否则,它将尝试为 printf() 复制原子变量本身,这显然不是本意。