【发布时间】:2014-12-07 14:22:49
【问题描述】:
此代码无法在 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) 从 MyStruct ctor 中删除 const 限定符(MyStruct( std::function<void()>& func) 给出了一个非常非常不同的错误:
1>f:\work\main\dev\common\teststdfunction\teststdfunction\teststdfunction.cpp(16): 错误 C2664:“MyStruct::MyStruct(const MyStruct &)”:无法转换 参数 1 从 'int' 到 'std::function &'
这比原来的错误更有意义,现在将 NULL 修复为 nullptr 并没有解决它。为什么 int(或 nullptr)拒绝转换为 std::function<>& 但同意转换为 const std::function<>& ?
2) 原始代码在 VS2010 中按预期编译和工作。它是一个不起眼的 VS2010 库错误吗?
编辑: 就 const/non 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并且可能是不同的值。使用nullptr而不是`NULL。 -
如前所述,我确实更改为 nullptr。我不是在问为什么原始代码没有编译。
-
文字根本不是 CONST。它们是右值。这是完全不同的事情。不要传播那个不正确的神话。
-
为什么要还原我的编辑?修改后的代码也可以在 VS 中运行,并且实际上是符合标准的。
-
@pmr - 你在问我吗?我没有恢复任何编辑,但我确实添加了我自己的编辑,这可能会意外超出您的编辑(如果我们同时进行的话)。我很抱歉。
标签: c++ visual-c++-2010 visual-c++-2013