如何打印std :: atomic <unsigned int>的值

Met*_*est 8 c++ printing atomic

std::atomic<unsigned int>在我的程序中使用.如何使用打印其值printf?因为如果我只是使用它不起作用%u.我知道我可以使用cout,但我的程序充满了printf电话,我不想替换它们.以前我使用的是unsigned int代替,std::atomic<unsigned int>所以我只是使用%u,因此打印工作正常.

Joh*_*ica 14

template<typename BaseType>
struct atomic
{
    operator BaseType () const volatile;
}
Run Code Online (Sandbox Code Playgroud)

使用类型转换来提取基础值.

printf("%u", unsigned(atomic_uint));
Run Code Online (Sandbox Code Playgroud)


dir*_*000 13

另一种选择,您可以使用负载。例如:

std::atomic<unsigned int> a = { 42 };
printf("%u\n", a.load());
Run Code Online (Sandbox Code Playgroud)