代码之家  ›  专栏  ›  技术社区  ›  ereOn

VS2010是否存在boost::bind问题?

  •  5
  • ereOn  · 技术社区  · 14 年前

    我有下面的代码行,它在2010年之前的G++和Visual Studio下编译得很好。

    std::vector<Device> device_list;
    
    boost::function<void (Device&, boost::posix_time::time_duration&)> callback = 
      boost::bind(&std::vector<Device>::push_back, &device_list, _1);
    

    在哪里? Device 是一门课,没什么特别的。

    现在我刚将我的Visual Studio版本升级到2010,编译失败,原因是:

    Error   1   error C2780: 'boost::_bi::bind_t<_bi::dm_result<MT::* ,A1>::type,boost::_mfi::dm<M,T>,_bi::list_av_1<A1>::type> boost::bind(M T::* ,A1)' : expects 2 arguments - 3 provided C:\developments\libsystools\trunk\src\upnp_control_point.cpp    95
    

    发生了什么事,我如何解决这个问题?

    谢谢。

    2 回复  |  直到 14 年前
        1
  •  10
  •   Steve Townsend    14 年前

    这可能是因为 vector::push_back 现在通过支持或C++0X特性有2个重载,使 bind 模棱两可的。

    void push_back(
       const Type&_Val
    );
    void push_back(
       Type&&_Val
    );
    

    这应该有效,或者使用@deadmg答案中建议的内置函数:

    std::vector<Device> device_list;
    
    boost::function<void (Device&, boost::posix_time::time_duration&)> callback = 
      boost::bind(static_cast<void (std::vector<Device>::*)( const Device& )>
      (&std::vector<Device>::push_back), &device_list, _1);
    
        2
  •  4
  •   Puppy    14 年前

    MSVC10中存在绑定问题。这不是我见过的第一篇报道它的问题的文章。其次,引入LAMBDAS是完全冗余的,而Boo::函数已经被STD::函数取代。

    std::vector<Device> devices;
    std::function<void (Device&, boost::posix_time::time_duration&)> callback = [&](Device& dev, boost::posix_time::time_duration& time) {
        devices.push_back(dev);
    };
    

    在MSVC10中不需要使用绑定。