VC++을 사용하여 프로그램을 컴파일하고 (Visual Studio 2015, 업데이트 3 사용) 일부 스 니펫은 컴파일되지 않습니다.std :: bind는 MSVC에서 std :: atomic_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
뭔가 내가 그리워하거나 컴파일러 잘못이 있습니까?
원자는 복사 할 수 없습니다. 그래서'std :: ref'가 필요합니다. –
@KerrekSB 왜 프로그램이 원자를 복사하려고합니까? 이 함수는 매개 변수로 참조를 가져오고, 원자는 참조로 전달되며, 참조 옆에 복사되는 내용을 볼 수 없습니다. –
C++ 유형 및 객체 모델에 대한 근본적인 오해가있는 것으로 보입니다. 당신은 절대로 "참조를 복사하십시오". 항상 * 값을 복사합니다. 값은 절대 참조가 아닙니다. 참조는 값을 지정하는 데 사용할 수있는 일종의 변수 *입니다. 그래서 여러분이'ref'라고 말할 때, 그것은'std :: atomic_bool' 타입의 값입니다 (그리고 그것은'b'와 같은 값이됩니다). 'std :: ref (b)'(또는'std :: ref (ref)')를 말할 때 그것은'std :: reference_wrapper'형의 값이고'std :: bind' 그것을 해석하기위한 프로토콜이 있습니다. –