gpt4 book ai didi

c++ - std::bind 无法在 MSVC 上使用 std::atomic_bool& 进行编译

转载 作者:行者123 更新时间:2023-11-30 02:27:42 35 4
gpt4 key购买 nike


我正在使用 VC++ 编译我的程序(使用 Visual Studio 2015,更新 3),但一些片段无法编译。

基本上,我想绑定(bind)一个函数,该函数使用原子 bool 值获取对原子 bool 值的引用。自包含代码:

void stub(std::atomic_bool& b) {
b = true;
}

int main() {
std::atomic_bool b(false);
std::function<void()> delegate = std::bind(stub, b); //fails to compile

auto& ref = b;
std::function<void()> delegate0 = std::bind(stub, ref); //fails to compile

std::function<void()> delegate1 = std::bind(stub, std::ref(b)); //compiled
/*...*/
}

编译器堆栈跟踪:

1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(357): error C2665: 'std::tuple<std::atomic<bool>>::tuple': none of the 2 overloads could convert all the argument types
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\tuple(608): note: could be 'std::tuple<std::atomic<bool>>::tuple(std::tuple<std::atomic<bool>> &&)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\tuple(607): note: or 'std::tuple<std::atomic<bool>>::tuple(const std::tuple<std::atomic<bool>> &)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility(357): note: while trying to match the argument list '(std::atomic<bool>)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(866): note: see reference to function template instantiation 'std::_Compressed_pair<void (__cdecl *)(std::atomic_bool &),std::tuple<std::atomic<bool>>,false>::_Compressed_pair<void(__cdecl &)(std::atomic_bool &),_Cv_TiD&>(std::_One_then_variadic_args_t,_Other1,_Cv_TiD &)' being compiled
1> with
1> [
1> _Cv_TiD=std::atomic<bool>,
1> _Other1=void (__cdecl &)(std::atomic_bool &)
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(864): note: see reference to function template instantiation 'std::_Compressed_pair<void (__cdecl *)(std::atomic_bool &),std::tuple<std::atomic<bool>>,false>::_Compressed_pair<void(__cdecl &)(std::atomic_bool &),_Cv_TiD&>(std::_One_then_variadic_args_t,_Other1,_Cv_TiD &)' being compiled
1> with
1> [
1> _Cv_TiD=std::atomic<bool>,
1> _Other1=void (__cdecl &)(std::atomic_bool &)
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(863): note: while compiling class template member function 'std::_Binder<std::_Unforced,void (__cdecl &)(std::atomic_bool &),std::atomic_bool &>::_Binder(_Fx,std::atomic_bool &)'
1> with
1> [
1> _Fx=void (__cdecl &)(std::atomic_bool &)
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(890): note: see reference to function template instantiation 'std::_Binder<std::_Unforced,void (__cdecl &)(std::atomic_bool &),std::atomic_bool &>::_Binder(_Fx,std::atomic_bool &)' being compiled
1> with
1> [
1> _Fx=void (__cdecl &)(std::atomic_bool &)
1> ]
1> c:\visual studio 2015\projects\quantum\quantum\main.cpp(658): note: see reference to class template instantiation 'std::_Binder<std::_Unforced,void (__cdecl &)(std::atomic_bool &),std::atomic_bool &>' being compiled

是我遗漏了什么还是编译器错误?

最佳答案

bind 始终尝试存储参数的,从不存储引用。 atomic 类型不能被复制。因此,当 bind 尝试复制它们时,它将失败。

这是 reference_wrapper 存在的原因之一:允许在需要值的地方使用对对象的引用。实际上,发明 std::ref 主要是为了处理 bind

看,bind 可以存储对参数的引用。但是,存储引用可能非常危险,尤其是对堆栈变量的引用可能会在调用 bind 仿函数之前的某个时间停止存在。所以 bind 强制您明确何时存储引用;它让你使用 ref

关于c++ - std::bind 无法在 MSVC 上使用 std::atomic_bool& 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408994/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com