boost::ref 没有发生匹配调用错误,但 std::ref 没有发生匹配调用错误
No matching call error occuring for boost::ref but not with std::ref
我编写了一些代码,它使用函子和来自boost::
或std::
(对于 C++11(命名空间的ref
和bind
模板来计算向量的元素数量。我正在使用#define
在boost::
和std::
命名空间之间切换。我使用的是 boost 版本 1.53,我的编译命令是 g++ test.cpp -std=c++11
.我尝试过 gcc 版本 4.7.2 和 4.6.3,但两者都遇到了相同的错误。
我有3个问题:
- 我不明白示例 2 生成的错误。
- 是否可以仅通过切换命名空间来使这样的代码具有可移植性?
- 有没有一个很好的参考资料详细描述
bind
、ref
和function
的std
和boost
版本之间的差异?(我看到了这个问题,但答案没有提到ref
或function
(
谢谢!
附言这个例子只是说明了我的问题,我知道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::bind
和std::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
提供了特殊处理。
- C++模板类 - 继承调用错误的函数
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 std::find 时没有匹配的函数调用错误
- 使用成员初始值设定项列表时没有匹配的函数调用错误
- C++调用错误签名的函数
- C++ 类成员调用错误 C3861
- C bool表达式作为函数参数调用错误的超载
- C ++尝试访问映射中的元素会给我一个不匹配的函数调用错误
- 当提供巨大的输入时,中止在堆栈操作程序中调用错误
- C++类静态成员函数调用错误
- 如何调查随机安卓原生函数调用错误?
- 引发实例后终止调用 - C++错误
- 如何修复传递参数时调用错误函数的主函数?(C++)
- 没有用于调用错误的匹配函数.将字符串推送到向量中
- 从括号内的initializer_list构造时,将调用错误的过载
- 为什么初始值设定项列表中的元素数会导致不明确的调用错误
- VC 15调用错误的复制构造函数以捕获lambda
- 在 C++ 中没有针对调用错误的匹配函数
- C++:函数调用错误:标识符"name"未定义,如果已定义?
- 为什么 GCC 在显式指定模板参数时会产生奇怪的错误并尝试调用错误的方法?