为什么bind1st和bind2nd需要常量函数对象?

rlb*_*ond 1 c++ templates stl

所以,我正在写一个C++程序,它可以让我控制整个世界.我写完了最后的翻译单元,但是我收到了一个错误:

error C3848: expression having type 'const `anonymous-namespace'::ElementAccumulator<T,BinaryFunction>' would lose some const-volatile qualifiers in order to call 'void `anonymous-namespace'::ElementAccumulator<T,BinaryFunction>::operator ()(const point::Point &,const int &)'
        with
        [
            T=SideCounter,
            BinaryFunction=std::plus<int>
        ]
        c:\program files (x86)\microsoft visual studio 9.0\vc\include\functional(324) : while compiling class template member function 'void std::binder2nd<_Fn2>::operator ()(point::Point &) const'
        with
        [
            _Fn2=`anonymous-namespace'::ElementAccumulator<SideCounter,std::plus<int>>
        ]
        c:\users\****\documents\visual studio 2008\projects\TAKE_OVER_THE_WORLD\grid_divider.cpp(361) : see reference to class template instantiation 'std::binder2nd<_Fn2>' being compiled
        with
        [
            _Fn2=`anonymous-namespace'::ElementAccumulator<SideCounter,std::plus<int>>
        ]
Run Code Online (Sandbox Code Playgroud)

我查看了它的规格,binder2nd它是:它采用了constAdaptibleBinaryFunction.

所以,我想,这不是什么大不了的事.我刚用过boost::bind,对吧?

错误!现在我的接管世界程序需要很长时间才能编译(bind在一个实例化很多的模板中使用)!按照这个速度,我的克星将首先接管世界!我不能让这种情况发生 - 他使用Java!

那么有人能告诉我为什么做出这个设计决定吗?这似乎是一个奇怪的决定.我想我现在必须制作课堂上的一些元素mutable......

编辑:违规代码:

template <typename T, typename BinaryFunction>
class ElementAccumulator 
    : public binary_function<typename T::key_type, typename T::mapped_type, void>
{
public:
    typedef T MapType;
    typedef typename T::key_type KeyType;
    typedef typename T::mapped_type MappedType;
    typedef BinaryFunction Func;

    ElementAccumulator(MapType& Map, Func f) : map_(Map), f_(f) {}

    void operator()(const KeyType& k, const MappedType& v)
    {
        MappedType& val = map_[k];
        val = f_(val, v);
    }
private:
    MapType& map_;
    Func f_;
};

void myFunc(int n)
{
    typedef boost::unordered_map<Point, int, Point::PointHash> Counter;
    Counter side_count;
    ElementAccumulator<SideCounter, plus<int> > acc(side_count, plus<int>());

        vector<Point> pts = getPts();
    for_each(pts.begin(), pts.end(), bind2nd(acc, n));
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lli 5

binder2nd构造函数需要一个恒定参考AdaptableBinaryFunction- 一个const AdaptableBinaryFunction本身.你的实例化代码怎么样?一个人通常没有明确提到binder2nd,而是通过便利函数bind2nd(它只x使用一个typename Operation::second_argument_type(x)或类似的第二个参数).