本文介绍了STL的普通一元函数适应一元谓词如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有功能


bool IsGood(const std :: string& sr);


我想在std中使用该功能:: not1 STL仿函数。我试过这个:


not1(IsGood)

/ *

错误:''std :: unary_negate< _Fn1> std :: not1(const _Fn1&)'':不能

从''overloaded

函数类型''推断''重载函数类型''的模板参数


错误:''std :: unary_negate< _Fn1> std :: not1(const _Fn1&)'':无法推断出来自''重载函数类型'的''重载函数类型'的
模板参数

类型''

* /


和这个


not1(ptr_fun(IsGood))

/ *

functional(209):warning:限定符适用于引用类型;忽略

功能(209):错误:''_ Left'':引用参考是非法的

* /


不工作,我不明白为什么。请帮忙。


TIA

I have function

bool IsGood (const std::string& sr);

I want to use that function in std::not1 STL functor. I tried this:

not1(IsGood)
/*
error : ''std::unary_negate<_Fn1> std::not1(const _Fn1 &)'' : could not
deduce template argument for ''overloaded function type'' from ''overloaded
function type''

error: ''std::unary_negate<_Fn1> std::not1(const _Fn1 &)'' : could not deduce
template argument for ''overloaded function type'' from ''overloaded function
type''
*/

and this

not1(ptr_fun(IsGood))
/*
functional(209): warning : qualifier applied to reference type; ignored
functional(209): error : ''_Left'' : reference to reference is illegal
*/

both don''t work and I don''t understand why. Please help.

TIA

推荐答案




好​​吧,看起来你可能已经超载了IsGood功能和

编译器无法弄清楚你的意思。试试:


ptr_fun(bool(* IsGood)(const std :: string&));


也可以,not1可能需要一些帮助返回类型:


std :: not1< bool>(ptr_fun(bool(* IsGood)(const std :: string&));


虽然确实知道

确切的问题是什么,但确实没有足够的代码发布。


Greg



Well, it looks like you may have overloaded the IsGood function and the
compiler cannot figure out which one you mean. Try:

ptr_fun(bool (*IsGood)(const std::string&));

also, not1 may need some assistance with the return type:

std::not1<bool>(ptr_fun(bool (*IsGood)(const std::string&));

There''s not really enough code posted though to know for sure what the
exact problem is though.

Greg




好吧,看起来你可能已经重载了I​​sGood函数,而
编译器无法找出你的意思。试试:

ptr_fun(bool(* IsGood)(const std :: string&));

此外,not1可能需要一些返回类型的帮助:

std :: not1< bool>(ptr_fun(bool(* IsGood)(const std :: string&));

发布的代码不够虽然知道确切的问题是什么。

Greg



Well, it looks like you may have overloaded the IsGood function and the
compiler cannot figure out which one you mean. Try:

ptr_fun(bool (*IsGood)(const std::string&));

also, not1 may need some assistance with the return type:

std::not1<bool>(ptr_fun(bool (*IsGood)(const std::string&));

There''s not really enough code posted though to know for sure what the
exact problem is though.

Greg




这里有更多的代码。我把它放在空项目中,所以它没有比这更好的了,而且仍然发生同样的错误。此外,你的命题都不起作用:


#include< string>

#include< list>

#include< algorithm>

#include< iostream>


使用命名空间std;


typedef list< string> ls;


bool IsGood(const string& st){

return st.empty(); //为了演示

}


int main()

{

ls tbl ;


tbl.push_back(" tgklhdfjh");

tbl.push_back("");

tbl .push_back("");

tbl.push_back("");

tbl.push_back("");

tbl.push_back("");

tbl.push_back(" gkjdhgdkfjgh");


ls :: iterator lbeg,lend ,new_end;

lbeg = tbl.begin();

lend = tbl.end();


//下一个一个是有问题的

new_end = remove_if(lbeg,lend,not1(ptr_fun(IsGood)));

//编译器给出以下内容:

// functional(209):warning:应用于引用类型的限定符;忽略

//功能性(209):错误C2529:''_左'':参考参考是

//非法


返回1;

}


我无法弄清楚我做错了什么......



Well here is some more code. I put this in empty project so there is
nothing more in it than this and still same error occurs. Also, none of
your propositions works:

#include <string>
#include <list>
#include <algorithm>
#include <iostream>

using namespace std;

typedef list<string> ls;

bool IsGood (const string& st) {
return st.empty(); // For sake of demonstration
}

int main ()
{
ls tbl;

tbl.push_back("tgklhdfjh");
tbl.push_back("");
tbl.push_back("");
tbl.push_back("");
tbl.push_back("");
tbl.push_back("");
tbl.push_back("gkjdhgdkfjgh");

ls::iterator lbeg, lend, new_end;
lbeg = tbl.begin();
lend = tbl.end();

// Next one is problematic
new_end = remove_if(lbeg, lend, not1(ptr_fun(IsGood)));
// Compiler gives following:
// functional(209): warning : qualifier applied to reference type; ignored
// functional(209): error C2529: ''_Left'' : reference to reference is
// illegal

return 1;
}

I just can''t figure out what I''m doing wrong...





我已经找到了部分问题。


如果我有


bool IsGood(const std :: string& sr);


我可以构造这种unary_function


pointer_to_unary_function< const string&,bool> functor_is_good(IsGood);


如果我像这样测试functor_is_good:


bool b_tmp;

string s_tmp;

b_tmp = functor_is_good(s_tmp);


b_tmp有预期的结果,并且没有发出警告。


但是,如果我尝试在not1中使用functor_is_good,那么:


not1(functor_is_good)


然后出现错误。问题是当unary_negate的模板是即时的时候,某个地方会出现&& (参考参考)一些

成员函数的参数(实际上当实例化const const string&&

时),并且报告为错误。现在,如果我改变


bool IsGood(const std :: string& sr);

进入

bool IsGood(const std :: string sr);




pointer_to_unary_function< const string&,bool> functor_is_good(IsGood);

进入

pointer_to_unary_function< const string,bool> functor_is_good2(IsGood);


然后表达


not(functor_is_good2)变得有效,一切正常并且符合预期。


这可能是标准化的行为吗?我的意思是,STL仿函数和

适配器不能适应使用pass by reference的函数看起来有点

对我来说很奇怪。它可能是我的编译器的STL实现中的错误吗?我没有在文档的任何部分找到,改编函数的参数不能作为参考:我看到的每个地方都说它们可以是任意类型。

我眼花缭乱,迷茫......任何人?



I have figured out part of the problem.

If I have

bool IsGood (const std::string& sr);

I am allowed to construct this kind of unary_function

pointer_to_unary_function<const string&,bool> functor_is_good (IsGood);

If I test functor_is_good like this:

bool b_tmp;
string s_tmp;
b_tmp = functor_is_good (s_tmp);

b_tmp has expected result, and no warnings are issued.

But, if I try to use functor_is_good in not1 like this:

not1(functor_is_good)

then errors arise. The problem is that when template for unary_negate is
instantied, somewhere occurs && (reference to reference) argument of some
member function (actually when instantiated const const string& &
occurs), and that is reported as error. Now, if I change

bool IsGood (const std::string& sr);
into
bool IsGood (const std::string sr);

and
pointer_to_unary_function<const string&,bool> functor_is_good (IsGood);
into
pointer_to_unary_function<const string,bool> functor_is_good2 (IsGood);

then expression

not(functor_is_good2) becomes valid and all works fine and as expected.

Is it possible that this is standardized behavior? I mean, STL functors and
adapters that can''t adapt functions that use pass by reference seem a bit
strange to me. Could it be a bug in my compiler''s STL implementation? I
didn''t find in any part of docs that arguments of adapted functions can''t
be reference: everywhere I looked it states they can be of arbitrary type.
I''m dazzled and confused... Anybody?


这篇关于STL的普通一元函数适应一元谓词如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 10:43