Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于将谓词作为模板参数传递_C++_Templates_Function Templates - Fatal编程技术网

C++ 关于将谓词作为模板参数传递

C++ 关于将谓词作为模板参数传递,c++,templates,function-templates,C++,Templates,Function Templates,我编写了一个通用函数模板,如下所示,它不会生成 仅当谓词通过常量引用或通过值传递时,它才会生成 你知道C++中的原理是什么吗? 我正在使用g++而我的版本没有可用的C++11 我已经检查了boost::bind返回的内容,它显示未指定类型(我希望已经正确阅读) struct MyRecord{ INTA_; int b_; }; 模板 布尔伊萨尼(谓词和pred){ vector::iterator it=std::find_if( 记录。开始(), records.end(), pred);

我编写了一个通用函数模板,如下所示,它不会生成

仅当谓词通过
常量引用
通过值
传递时,它才会生成

你知道C++中的原理是什么吗? 我正在使用g++而我的版本没有可用的C++11

我已经检查了
boost::bind
返回的内容,它显示
未指定类型
(我希望已经正确阅读)

struct MyRecord{
INTA_;
int b_;
};
模板
布尔伊萨尼(谓词和pred){
vector::iterator it=std::find_if(
记录。开始(),
records.end(),
pred);
if(it!=records.end()){
//做点什么
}
返回它!=records.end();
}
isAny(boost::bind(&MyRecord::a_u,u 1)==1);
isAny(boost::bind(&MyRecord::b_,_1)==2);
//它适用于下面的2个声明
模板
bool-isAny(const谓词和pred);
//或
模板
bool-isAny(谓词pred);
您的呼叫
是任意的(boost::bind(&MyRecord::a_u,u 1)==1)创建一个临时对象。在C++中,临时只能绑定到const引用参数或值参数。其思想是,如果函数通过非常量引用获取参数,则意味着修改参数,而修改临时参数将毫无意义

FWIW STL按值获取谓词。

您的调用
isAny(boost::bind(&MyRecord::a_,_1)==1)创建一个临时对象。在C++中,临时只能绑定到const引用参数或值参数。其思想是,如果函数通过非常量引用获取参数,则意味着修改参数,而修改临时参数将毫无意义

FWIW STL按值获取谓词。

您的调用
isAny(boost::bind(&MyRecord::a_,_1)==1)创建一个临时对象。在C++中,临时只能绑定到const引用参数或值参数。其思想是,如果函数通过非常量引用获取参数,则意味着修改参数,而修改临时参数将毫无意义

FWIW STL按值获取谓词。

您的调用
isAny(boost::bind(&MyRecord::a_,_1)==1)创建一个临时对象。在C++中,临时只能绑定到const引用参数或值参数。其思想是,如果函数通过非常量引用获取参数,则意味着修改参数,而修改临时参数将毫无意义


FWIW STL按值获取谓词。

为什么不
isAny([](const MyRecord&record){return record.a_==1;})?抱歉。忘记指定我没有可用的C++11。为什么不
isAny([](const MyRecord&record){return record.a_==1;})?抱歉。忘记指定我没有可用的C++11。为什么不
isAny([](const MyRecord&record){return record.a_==1;})?抱歉。忘记指定我没有可用的C++11。为什么不
isAny([](const MyRecord&record){return record.a_==1;})?抱歉。忘记指定我没有可用的C++11。非常感谢。我没想过。非常感谢你。我没想过。非常感谢你。我没想过。非常感谢你。我还没想过。
 struct MyRecord{
   int a_;
   int b_;
 };

 template<class Predicate>
 bool isAny(Predicate &pred) {
  vector<MyRecord>::iterator it = std::find_if(
           records.begin(), 
           records.end(), 
           pred);
  if(it!=records.end()) {
   // .. do something
  }
  return it != records.end();
 }

 isAny(boost::bind(&MyRecord::a_,_1)==1);
 isAny(boost::bind(&MyRecord::b_,_1)==2);

 // It works with the 2 declarations below
 template<class Predicate>
 bool isAny(const Predicate &pred);

 // or

 template<class Predicate>
 bool isAny(Predicate pred);