本文介绍了std ::绑定在std ::函数的向量在VC10编译,但不是VC11。我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当从VC10移动到VC11时,以下代码无法编译。
When moving from VC10 to VC11, the following code fails to compile.
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
using namespace std::placeholders;
typedef std::function<void(int)> CB;
int main()
{
vector<CB> m_CBs;
int m_LongPressGesture;
for_each(m_CBs.begin(), m_CBs.end(), bind(&CB::operator(), _1, m_LongPressGesture));
return 0;
}
错误输出是非法间接。
这是完整的输出。
The error output is illegal indirection.Here's the complete output.
1>------ Build started: Project: CompileError, Configuration: Debug Win32 ------
1>Build started 2/12/2013 4:32:56 PM.
1>InitializeBuildStatus:
1> Touching "Debug\CompileError.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1269): error C2100: illegal indirection
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1152) : see reference to function template instantiation '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<std::function<_Fty>>(_Wrapper &,_V0_t) const' being compiled
1> with
1> [
1> _Rx=void,
1> _Pmf_t=void (__thiscall std::_Func_class<void,int>::* )(int) const,
1> _Farg0=std::_Func_class<void,int>,
1> _V0_t=int,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _Fty=void (int),
1> _Wrapper=std::function<void (int)>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1152) : see reference to function template instantiation '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<std::function<_Fty>>(_Wrapper &,_V0_t) const' being compiled
1> with
1> [
1> _Rx=void,
1> _Pmf_t=void (__thiscall std::_Func_class<void,int>::* )(int) const,
1> _Farg0=std::_Func_class<void,int>,
1> _V0_t=int,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _Fty=void (int),
1> _Wrapper=std::function<void (int)>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(24) : see reference to function template instantiation 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<std::function<_Fty>&>(std::function<_Fty>)' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall std::_Func_class<void,int>::* )(int) const,void,std::_Func_class<void,int>,int,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Ph<1> &,
1> _V1_t=int &,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _Fty=void (int)
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(24) : see reference to function template instantiation 'void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<std::function<_Fty>&>(std::function<_Fty>)' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall std::_Func_class<void,int>::* )(int) const,void,std::_Func_class<void,int>,int,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Ph<1> &,
1> _V1_t=int &,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _Fty=void (int)
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(33) : see reference to function template instantiation 'void std::_For_each<std::function<_Fty>*,_Fn1>(_InIt,_InIt,_Fn1 &)' being compiled
1> with
1> [
1> _Fty=void (int),
1> _Fn1=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall std::_Func_class<void,int>::* )(int) const,void,std::_Func_class<void,int>,int,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,std::_Ph<1> &,int &,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _InIt=std::function<void (int)> *
1> ]
1> c:\users\jedwards\documents\visual studio 2012\projects\compileerror\compileerror\main.cpp(16) : see reference to function template instantiation '_Fn1 std::for_each<std::_Vector_iterator<_Myvec>,std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>>(_InIt,_InIt,_Fn1)' being compiled
1> with
1> [
1> _Fn1=std::_Bind<true,void,std::_Pmf_wrap<void (__thiscall std::_Func_class<void,int>::* )(int) const,void,std::_Func_class<void,int>,int,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,std::_Ph<1> &,int &,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _Myvec=std::_Vector_val<std::_Simple_types<std::function<void (int)>>>,
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall std::_Func_class<void,int>::* )(int) const,void,std::_Func_class<void,int>,int,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=std::_Ph<1> &,
1> _V1_t=int &,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::function<void (int)>>>>
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.68
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我可以想出的唯一的事情是bind是期望一个指针。我尝试了ref()但没有改变。
The only thing I can come up with is bind is expecting a pointer. I tried ref() but made no difference.
谢谢,
Jim
Thanks,Jim
推荐答案
因为VC显然有一个错误(感谢Stephan!),你可以写循环:
Since VC apparently has a bug (thanks Stephan!), you could write the loop as:
for ( auto &cb : m_CBs ) cb ( m_LongPressGesture );
或:
for_each ( m_CBs.begin(), m_CBs.end(),
[m_LongPressGesture] ( const CB &cb ) { cb ( m_LongPressGesture ); } );
这篇关于std ::绑定在std ::函数的向量在VC10编译,但不是VC11。我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!