이 코드는 VC2013 구축하는 데 실패 : (편집 : 그것은 빌드 실패 이유를 묻는 게 아니에요) 오류와실패 :: 기능 <> &
#include <functional>
struct MyStruct
{
std::function<void()> m_Func;
MyStruct(const std::function<void()>& func) : m_Func(func) {}
};
int main()
{
MyStruct rc(NULL);
return 0;
}
:
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xrefwrap(283): error C2064: term does not evaluate to a function taking 0 arguments
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Rx=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(226) : while compiling class template member function 'void std::_Func_impl<_MyWrapper,_Alloc,_Ret,>::_Do_call(void)'
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=int
1> , _Fty=int
1> ]
1> f:\work\teststdfunction\teststdfunction.cpp(16) : see reference to function template instantiation 'std::function<void (void)>::function<int>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=int
1> ]
1> f:\work\teststdfunction\teststdfunction.cpp(16) : see reference to function template instantiation 'std::function<void (void)>::function<int>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=int
1> ]
(마지막 두 개의보고 된 오류에서 '_Fx = int'에 유의하십시오.)
MyStruct rc(NULL)
에서 MyStruct rc(nullptr)
으로 변경하면 오류가 해결되므로 함께 살 수 있습니다. 지금
원래 오류보다 더 많은 의미가1>f:\work\main\dev\common\teststdfunction\teststdfunction\teststdfunction.cpp(16): error C2664: 'MyStruct::MyStruct(const MyStruct &)' : cannot convert argument 1 from 'int' to 'std::function &'
하고 NULL을 고정 : MyStruct(std::function<void()>& func)
은 매우, 매우 다른 오류를 제공 (
1) MYSTRUCT의의 ctor에서 const를 한정자를 제거 : 두 가지 그러나 신비 남아 그것을 해결하지 않습니다 을 nullptr 할 수 있습니다. 왜 INT (또는 nullptr)는 std::function<>&
캐스팅을 거부하지만, 원래의 코드를 컴파일하고 VS2010에서 예상대로 작동)을 const std::function<>&
?
2에 캐스팅 동의 않습니다. 애매한 VS2010 라이브러리 버그입니까?
는편집 : 는 지금까지 CONST/비 const를 질문은 간다, 지금 주조 참여하고 잠재적 인 유형 불일치 아마 빨간 청어 생각합니다. 전달 된 인수 - NULL 또는 nullptr -은 리터럴이므로 const입니다. 그냥 const가 아닌 참조에 바인딩 할 수 없습니다. 예 :
const int& a = 8; // Ok
int& b = 9; // error C2440: 'initializing' : cannot convert from 'int' to 'int &'
맞습니까? 나는 아직도 뭔가를 놓치고 있습니까?
'nullptr'은'nullptr_t' 타입의 값으로,'std :: function'의 생성자가 받아 들일 수 있습니다. 'NULL'은 반드시'nullptr'로 정의되는 것은 아니며 다른 값이 될 수도 있습니다. NULL이 아닌'nullptr'을 사용하십시오. –
언급 한대로, 나는 nullptr로 변경했습니다. 원래 코드가 컴파일되지 않은 이유를 묻지 않습니다. –
리터럴은 완전히 다릅니다. 그들은 rvalues입니다. 이것은 완전히 다른 것입니다. 그 잘못된 신화를 전파하지 마십시오. – Puppy