我试图使用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;
};