2

此代码无法在 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 ::功能 &'

这比原始错误更有意义,现在将 NULL 修复为 nullptr不能解决它。为什么 int (或 nullptr )拒绝转换为std::function<>&但同意转换为 a 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 &' 

听起来对吗?我还缺少什么吗?

4

2 回答 2

2

这里的特殊之处与构造函数模板约束和其他隐式转换有关。

起作用的原因nullptr是因为std::function有一个特定的构造函数使用它。此构造函数将始终是该参数的最佳匹配,因为函数模板的优先级较低,其他条件相同。

通常,0将隐式转换为nullptr,这很好。问题是它可以传递给从函数对象构造的不受约束的函数模板构造函数。这不需要隐式转换,因此所有都不相等,因此首选此构造函数 - 这会导致您看到的错误int不是有效的函数对象。

libstdc++ 和 libc++ 没有表现出这种行为,因为它们已经针对这个问题实现了 C++14 修复,这限制了构造函数。C++11 没有,所以这种行为非常符合 C++11 的实现。

这种问题就是为什么NULL你不应该使用一个可怕的东西。事实上,VS2010 团队不得不nullptr在最后一刻作为一个附加特性匆忙推出,因为它NULL与 C++ 中的所有其他特性交互非常糟糕,尤其是与 C++11。

对于const与非const参考文献,其他答案已充分解释了该问题。

在没有 C++14 中提供的受限构造函数修复的情况下使用时,您可以找到其他 WTF——std::function这不是唯一的。总而言之,它是 C++11 标准中的缺陷,而不是VS中的缺陷。VS2010 编译它可能是一个编译器重载解析错误。

于 2014-12-07T14:59:22.650 回答
0

将 aT转换为 aU&永远行不通,这是故意的。您可以转换为U const&. 原因是临时 U 对象的更改不会传播回 T 值。

VS2010 在这方面有点问题,并且确实允许强制转换(但使用正确的设置,会发出警告)

于 2014-12-07T14:28:41.430 回答