我有以下代码:
#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'
有人可以帮忙吗?
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
方法,这可能更容易。