简体   繁体   English

为什么TBB无法将`int`转换为`const tbb :: atomic <unsigned int> &`,但std :: atomic可以吗?

[英]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>& . 阻止构建的最后一件事(?)是TBB无法处理将int强制转换为const tbb::<unsigned int>& The annoying thing is that the same cast using std::atomic (specifically const std::atomic<unsigned int>& ) works just fine. 令人讨厌的是,使用std::atomic (特别是const std::atomic<unsigned int>& )的相同std::atomic效果很好。 I can't refactor the code to use std instead of tbb (it uses other features of tbb that aren't part of std ). 我无法重构代码以使用std而不是tbb (它使用tbb其他功能,这些功能不属于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)? 有谁知道如何解决此问题(不删除对TBB的使用)? I need it to work in VS2017. 我需要它才能在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 . 编辑:另外,我得到以下错误: 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? 是否对tbb/atomic.h了编辑以启用此投射?

Fixed! 固定! The problem was with the pre-processor defines being out of date for the NuGet distro of TBB I was using. 问题在于预处理程序定义对于我正在使用的TBB的NuGet发行版已经过时。 VS2017 version 15.3.2 has support for constexpr , which is needed to enable __TBB__ATOMIC_CTORS . VS2017版本15.3.2支持constexpr ,启用__TBB__ATOMIC_CTORS是必需的。 Thanks to @StoryTeller for pointing me in the right direction. 感谢@StoryTeller为我指出正确的方向。

Fix: Git clone latest src of TBB and build. 修复:Git克隆了TBB的最新src并构建。 (Funny how shortcuts in C++ rarely are shortcuts). (有趣的是,C ++中的快捷方式很少是快捷方式)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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