我正在使用VC++编译我的程序(使用Visual Studio 2015,更新3),并且某些代码段无法编译。std :: bind无法与std :: atomic_bool和MSVC编译
基本上,我想绑定一个函数,该函数获取对原子布尔值的引用与原子布尔值。自包含的代码:
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
有什么我错过或它的编译器的错吗?
原子不可复制。这就是为什么你需要'std :: ref'。 –
@KerrekSB为什么程序试图复制原子?该函数获取参数作为参数,原子作为参考传递,看不到在参考旁边被复制的内容 –
看起来,您对C++类型和对象模型存在根本的误解。你*永远*“复制引用”。你总是*复制值*。值从来不是参考。引用是一种*变量*,您可以使用它来指定值。所以,当你说'ref'时,这是'std :: atomic_bool'类型的值(它恰好与'b'的值相同)。当你说'std :: ref(b)'(或'std :: ref(ref)')时,这是一个'std :: reference_wrapper'和'std :: bind'有一个协议来解释这一点。 –