在常量成员函数中使用 bind1st

use bind1st in const memeber function

本文关键字:bind1st 函数 常量 成员      更新时间:2023-10-16
ClassA & operator << ( ClassA &, int32_t )
{
    ...
}

class ClassMain
{
public:
    insert( ClassA & c ) const;
    ...
private:
    std::set<int> m_setContainer;
};
struct  InsertOpt : binary_function<ClassA, int, ClassA&>
{
        ClassA & operator( )( ClassA & c, int val ) const
        {
                c << val;
                return c;
        }
};
void ClassMain::insert( ClassA & c ) const
{
    // Case I: the for loop works
    for ( std::set<int>::const_iterator iter = m_setContainer.begin( );
          iter != m_setContainer.end( ); ++iter )
    {
            c << *iter; // operator<<( c, *iter );
    }
    // Case II: doesn't work
    for_each( m_setContainer.begin( ), m_setContainer.end( ), bind1st( InsertOpt(), c ) );

}
Error:
../include/c++/4.1.2/bits/stl_function.h:406: error: no match for call to '(const InsertOpt) (const ClassA&, const int&)'
note: candidates are: ClassA& InsertOpt::operator()(ClassA&, int32_t) const

问题>为什么编译器查找(const ClassA&, const int&)而不是ClassA & operator( )( ClassA & c, int val ) const

谢谢

"通常,传递给unary_function或 binary_function去掉了常量和参考资料。

这是不正确的。这意味着一些衰减应用于binary_function的模板参数(例如通过std::decay)。但该标准在[depr.base]中非常明确地定义了binary_function

template <class Arg1, class Arg2, class Result>
struct binary_function
{
    typedef Arg1 first_argument_type;
    typedef Arg2 second_argument_type;
    typedef Result result_type;
};

binder1st在 [depr.lib.binder.1st] 中定义:

template <class Fn>
class binder1st : public unary_function<typename Fn::second_argument_type,
                                        typename Fn::result_type>
{
protected:
    Fn op;
    typename Fn::first_argument_type value;
public:
    binder1st(const Fn& x,
              const typename Fn::first_argument_type& y);
    typename Fn::result_type
    operator()(const typename Fn::second_argument_type& x) const;
    typename Fn::result_type
    operator()(typename Fn::second_argument_type& x) const;
};
  1. 构造函数使用 x 初始化op,使用 y 初始化值。

  2. operator()返回op(value,x) .

如您所见,存储函数对象的参数是 value ,其类型为 typename Fn::first_argument_type但也要注意operator()是如何标记const内部的。成员value作为const对象传递,这会导致错误消息,因为InsertOpt只接受非const左值作为第一个参数。

但是,当第一个参数类型作为左值引用给出时,引用折叠规则在通过const访问路径访问value时适用,并且生成的value类型是"对非常量ClassA的左值引用"。

即使进行此更改,编译器也会生成相同的错误消息。

为我编译。

您将错误的类型传递到binary_function

struct  InsertOpt : binary_function<ClassA, int, ClassA&>
 //                                 ^^^^^^
{
        ClassA & operator( )( ClassA & c, int val ) const
        {
                c << val;
                return c;
        }
};

您的operator()需要ClassA&但您说这需要ClassA。那些需要排队:

struct  InsertOpt : binary_function<ClassA&, int, ClassA&>
{
    // rest as before
};

下面是一个小得多的例子:

struct InsertOpt :
    std::binary_function<int, int, int&>
    //                   ^^^ if you change this to int&, it compiles
{
    int& operator()(int& a, int b) const {
        std::cout << b << std::endl;
        return a;
    }
};
int main() {
    const std::vector<int> v = {1, 2, 3, 4};
    int a = 5;
    std::for_each(v.begin(), v.end(), std::bind1st(InsertOpt(), a));
}