boost::ref 没有发生匹配调用错误,但 std::ref 没有发生匹配调用错误

No matching call error occuring for boost::ref but not with std::ref

本文关键字:调用 错误 ref std boost      更新时间:2023-10-16

我编写了一些代码,它使用函子和来自boost::std::(对于 C++11(命名空间的refbind模板来计算向量的元素数量。我正在使用#defineboost::std::命名空间之间切换。我使用的是 boost 版本 1.53,我的编译命令是 g++ test.cpp -std=c++11 .我尝试过 gcc 版本 4.7.2 和 4.6.3,但两者都遇到了相同的错误。

我有3个问题:

  1. 我不明白示例 2 生成的错误。
  2. 是否可以仅通过切换命名空间来使这样的代码具有可移植性?
  3. 有没有一个很好的参考资料详细描述bindreffunctionstdboost版本之间的差异?(我看到了这个问题,但答案没有提到reffunction(

谢谢!

附言这个例子只是说明了我的问题,我知道size() std::vector:-(

//#define USE_STD
#ifdef USE_STD
#include <functional>
using namespace std::placeholders;
namespace impl = std;
#else
#include <boost/version.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
namespace impl = boost;
#endif
#include <iostream>
#include <algorithm>
#include <vector>
class Item {
    int id_;
public:
    Item(int id) : id_(id) {};
};
template <typename ITEM>
class Counter {
    int count_;
public:
    // typedef void result_type; // adding this fixes Example 3 when impl=boost
    Counter() : count_(0) {};
    void operator()(ITEM* item) {count_++;}
    int operator()() {return count_;}
};
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
#ifndef USE_STD
    std::cout << "BOOST_LIB_VERSION=" << BOOST_LIB_VERSION << std::endl;
#endif
    // allocate
    typedef std::vector<Item*> ItemVec;
    ItemVec vec;
    for (int i = 0; i < 9; ++i) {vec.push_back(new Item(i));}
    // Example 1, works for BOTH
    Counter<Item> f1;
    f1 = std::for_each(vec.begin(), vec.end(), f1);
    std::cout << "f1()=" << f1() << std::endl;
    // Example 2, works with impl=std ONLY
    // COMPILE ERROR with impl=boost: "no match for call to ‘(boost::reference_wrapper<Counter<Item> >) (Item*&)’"
    Counter<Item> f2;
    std::for_each(vec.begin(), vec.end(), impl::ref(f2));
    std::cout << "f2()=" <<  f2() << std::endl;
    // Example 3, works with impl=std ONLY
    // COMPILE ERROR with impl=boost "no type named ‘result_type’ in ‘class Counter<Item>’"
    // this can fixed by adding the typedef described above
    Counter<Item> f3;
    std::for_each(vec.begin(), vec.end(), impl::bind(impl::ref(f3), _1));
    std::cout << "f3()=" << f3() << std::endl;
    // clean up
    for (ItemVec::iterator it = vec.begin(); it != vec.end(); ++it) {
        delete *it;
    }
    vec.clear();
    return 0;
}

示例 2 失败,因为boost::reference_wrapper没有转发参数的成员operator(),这与std::reference_wrapper不同。因此,它只对通过引用传递普通参数有用,而不是预期调用的函数或函子。

示例 3 失败,因为 Boost.Bind 依赖于特定协议来获取您传递的函数或函子的结果类型(如果您使用没有显式返回类型的版本(。如果向其传递指向函数的指针或指向成员函数的指针,则返回的绑定器对象会将嵌套result_type设置为所述 PTF 或 PTMF 的返回类型。如果传递函子,它需要一个嵌套的result_type
另一方面,std::bind ,如果你的函子没有嵌套result_type,则根本没有嵌套。

请注意,正如我所说,您可以显式地向boost::bindstd::bind提供结果类型:

std::for_each(vec.begin(), vec.end(), impl::bind<void>(impl::ref(f3), _1));
//                                              ^^^^^^

这修复了示例并使其编译。

std::ref

boost::ref有一个主要优势: 它提供了一个无规律的完美转发operator(),它将调用转发到它所包含的引用上。

boost::ref实际上无法做到这一点,因为它需要大量的重载。 但是,为了允许这一点,boost::bind(和其他一些类(都为boost::reference_wrapper提供了特殊处理。