std::all_of not accepting class member function as function with 1 argument

…衆ロ難τιáo~ 提交于 2019-12-09 23:55:32

问题


I can't figure what I am doing wrong with this std::all_of call.

I have a class Statistics:

class Statistics {
public:
bool isDataSet() const { return m_data.size() > 0; }
private:
std::vector<double> m_data;
};

Each instance of Statistics class corresponds to a certain object.

In another function in a different file I want to display statistics only if data has been initialized in all Statistics instances. I want to use std::all_of function in the following manner:

if( std::all_of(m_stats.begin(), m_stats.end(), &Statistics::isDataSet) ) {
...
}

where std::vector<Statistics*> m_stats.

The compiler reports error in that the 'predicate term does not evaluate to a function taking 1 arguments'. As far as I know, each class member passes this pointer as the first parameter, so Statistics::isDataSet() should actually be a function with 1 parameter. But std::all_of sees this wrong.

Am I wrong in my assumption that Statistics::isDataSet() should be accepted as a function with 1 parameter in std::all_of()?


回答1:


use

std::bind(&Statistics::isDataSet, std::placeholders::_1)

or

[](const Statistics& s) { return s.isDataSet(); }

instead of &Statistics::isDataSet in call to all_of. latter expect a callable type (as predicate) and will pass an instance of Statistics to it. Specifying member-function w/o instance obviously not enough to make a call



来源:https://stackoverflow.com/questions/14461204/stdall-of-not-accepting-class-member-function-as-function-with-1-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!