atomic_fetch_add关于uint64的奇怪行为

问题描述 投票:0回答:1

我有以下代码:

#include "atomic"

std::atomic<uint64_t>bid_index(0);
uint64_t generate_bid_key(){
  return std::atomic_fetch_add(&bid_index,1);
}

我有以下错误:

No matching function for call to 'atomic_fetch_add' candidate template ignored: 
deduced conflicting types for parameter '_Tp' ('unsigned long long' vs. 'int') 
candidate template ignored: deduced conflicting types for 
parameter '_Tp' ('unsigned long long' vs. 'int') 
candidate template ignored: could not match 'type-parameter-0-0 *' against 
'unsigned long long' candidate template ignored: could not match 'type-parameter-0-0 *' 
against 'unsigned long long'

有人可以帮忙吗?

c++
1个回答
1
投票

std::atomic_fetch_add从第二个参数1(它是一个int)推导出操作数的模板类型,它与原子的模板化类型(它是uint64_t)不匹配。将其设为uint64_t,它将被接受。

return std::atomic_fetch_add(&bid_index,1ULL); 

return std::atomic_fetch_add(&bid_index,(uint64_t)1);

您还可以使用原子类的更直接的fetch_add方法,这可能更容易。

© www.soinside.com 2019 - 2024. All rights reserved.