在常量成员函数中使用 bind1st
use bind1st in const memeber function
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; };
构造函数使用
x
初始化op
,使用y
初始化值。
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));
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 为什么 std::bind1st 不适用于自由函数
- 在常量成员函数中使用 bind1st