首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::functions的向量上的Std::bind在VC10中编译,但不在VC11中编译。我做错什么了?

std::functions的向量上的Std::bind在VC10中编译,但不在VC11中编译。我做错什么了?
EN

Stack Overflow用户
提问于 2013-02-13 00:52:02
回答 2查看 1.3K关注 0票数 1

从VC10迁移到VC11时,以下代码无法编译。

代码语言:javascript
复制
#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;
}

错误输出是非法的间接寻址。下面是完整的输出。

代码语言:javascript
复制
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 ==========

我能想到的唯一办法就是绑定一个指针。我尝试了ref(),但没有任何效果。

谢谢,吉姆

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-13 06:02:36

因为VC显然有一个bug (谢谢斯蒂芬!),你可以把循环写成:

代码语言:javascript
复制
for ( auto &cb : m_CBs ) cb ( m_LongPressGesture );

或者:

代码语言:javascript
复制
for_each ( m_CBs.begin(), m_CBs.end(), 
             [m_LongPressGesture] ( const CB &cb ) { cb ( m_LongPressGesture ); } );
票数 1
EN

Stack Overflow用户

发布于 2013-02-13 05:40:57

听起来像http://connect.microsoft.com/VisualStudio/feedback/details/763571/,它目前被分配给我。

请注意,从技术上讲,不允许使用大多数标准库成员函数的地址(因为实现可以有额外的重载和默认参数)。

一个实际上会产生更好代码的变通方法是使用lambdas。在99%的情况下,Lambdas取代bind(),剩下的1%更好地使用手写functor处理。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14837603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档