作为boost::bind复合链的一部分的解引用迭代器
Dereference iterator as part of a boost::bind composite chain
我试图使用bind生成一个函数:
- 接收地图m
- 返回m.begin() ->第一个
为此,我试图使用boost::bind:
typedef map<int,int>::const_iterator (map<int,int>::*const_begin_end) () const;
bind(&pair<const int,int>::first, bind(static_cast<const_begin_end>(&map<int, int>::begin), _1));
这不起作用,因为begin的结果需要解引用。我想像
bind(&pair<const int,int>::first, bind(&operator*, bind(static_cast<const_begin_end>(&map<int, int>::begin), _1)));
但是这不起作用,因为没有全局操作符*。
问题:
- 是否有可能使用boost::bind复合链来实现这一点?如何?
- 更容易读的替代品?
我强烈推荐Boost。Phoenix是我在c++ 03中动态编写函子时的首选库。它是Boost的一个更好的替代品。Bind——那个库显示了它的年龄
例如,Phoenix允许我们在其函子上使用操作符来表示调用该函子时该操作符的实际用法。因此,arg1 + arg2
是一个函子,它返回前两个操作数的和。这大大降低了bind
噪声。第一次尝试可以像这样:
bind(&pair<const int, int>::first
, *bind(static_cast<const_begin_end>(&map<int, int>::begin), arg1)) )
(LWS演示)
但是凤凰的另一个优点是它带有一些电池。在我们的例子中,我们对<boost/phoenix/stl/container.hpp>
非常感兴趣,因为它包括一些熟悉的容器操作的惰性版本,包括begin
。这在我们的例子中非常方便:
// We don't need to disambiguate which begin member we want anymore!
bind(&pair<const int, int>::first, *begin(arg1))
(LWS演示)
最后,我要补充一点,c++ 11绑定表达式的指定使得指针到成员可以作用于任何使用
operator*
的。所以你可以这样做:
bind(&pair<const int, int>::first, bind(static_cast<begin_type>(&std::map<int, int>::begin), _1))
(LWS演示)
可以用成员函数指针调用bind,成员操作符就是成员函数:
const_begin_end pBegin = &map<int,int>::begin;
x = bind(&std::pair::first,
bind(&std::map<int, int>::const_iterator::operator*,
bind(pBegin, _1)
);
但说真的,你也可以写一个合适的函数来做你需要的事情,而不是那种不可读的增强。绑定混乱(你能说"可维护性"吗?)
因此,对于c++ 03,函数:
template <class Map>
typename Map::key_type keyBegin(Map const& m)
{
return m.begin().first;
}
或c++ 03的函子(可以在函数内部本地定义)
struct KeyBegin
{
typedef std::map<int, int> IntMap;
int operator()(IntMap const& m) {
return m.begin().first;
}
};
或c++ 11 lambda(比bind更可读):
auto keyBegin = [](std::map<int, int> const& m) -> int {
return std::begin(m).first;
};
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 我可以在循环中引用 std::string 的一部分并将其放在外面吗?
- C++的新手,程序在取消引用向量的一部分时没有响应
- 对象或引用的一部分
- 清楚地说明 * 和 & 作为类型的一部分和作为取消引用/address_of操作符之间的区别
- 引用从end()中减去的std::向量的一部分可以吗
- 引用静态函数作为类定义的一部分是否有任何开销?
- 作为boost::bind复合链的一部分的解引用迭代器
- C# 通过引用将数组的一部分作为函数的参数传递