简体   繁体   中英

Why can't TBB cast `int` to `const tbb::atomic<unsigned int>&`, but std::atomic can?

I'm attempting to build a large project that has many dependencies. The last thing (?) preventing it from building is TBB's failure to handle casting of an int into an const tbb::<unsigned int>& . The annoying thing is that the same cast using std::atomic (specifically const std::atomic<unsigned int>& ) works just fine. I can't refactor the code to use std instead of tbb (it uses other features of tbb that aren't part of std ).

I've created the following simple test case:

#include <tbb/atomic.h>
#include <atomic>

void good(const std::atomic<unsigned int>& i) {
}

void bad(const tbb::atomic<unsigned int>& i) {
}

int main() {
    good(1);
    bad(1); // error C2664: 'void bad(const tbb::atomic<unsigned int> &)': cannot convert argument 1 from 'int' to 'const tbb::atomic<unsigned int> &'
}

Does anyone know how to fix this (without removing use of TBB)? I need it to work in VS2017.

Edit: Also, I get the following error: Error (active) E0415 no suitable constructor exists to convert from "int" to "tbb::atomic<unsigned int>" Testmain.cpp 15 . So presumably, if there were a suitable c'tor, the cast would succeed. How can I add one? Is there an edit to tbb/atomic.h that would enable this cast?

Fixed! The problem was with the pre-processor defines being out of date for the NuGet distro of TBB I was using. VS2017 version 15.3.2 has support for constexpr , which is needed to enable __TBB__ATOMIC_CTORS . Thanks to @StoryTeller for pointing me in the right direction.

Fix: Git clone latest src of TBB and build. (Funny how shortcuts in C++ rarely are shortcuts).

从源代码中(我在https://github.com/01org/tbb/blob/tbb_2017/include/tbb/atomic.h中发现我自己没有TBB)我可以看到struct atomic仅具有一个赋值运算符定义,因此没有非明确的构造函数,这意味着您必须使用tbb::make_atomic显式构造它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM