功能对象 [英] Function objects

查看:77
本文介绍了功能对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码,


#include< iostream>


struct Functor {

void operator()(void){std :: cout<< "函子" <<的std :: ENDL; }

};


int

main(无效)

{

Functor();

返回0;

}


为什么运营商()不需要是否创建了临时对象?


-

anderberg:人们只是突变。有些比其他人差。

解决方案

* anderberg:

请考虑以下代码,

#include< iostream>

struct Functor {
void operator()(void){std :: cout<< "函子" <<的std :: ENDL; }


''void''正式参数列表是C-ism;不要。


此外,应该是''const''。


};

int
main(void)


''void''正式参数列表是C-ism;不要。


{
Functor();
返回0;


''main''中没有必要,因为''main''是一项特殊功能。


}

为什么操作符()没有调用'创建的临时对象?




为什么要这样?


尝试Functor()()。


-

答:因为它弄乱了人们通常阅读文本的顺序。 />
问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?电子邮件?


anderberg写道:

考虑以下代码,

#include< iostream>

struct Functor {
void operator()(void){std :: cout<< "函子" <<的std :: ENDL; }
};

int
main(无效)
{
Functor();
返回0;
}

为什么operator()没有调用''​​创建的临时对象?




因为你实际上并没有*打电话给它。


Functor()


只是创建一个临时对象。现在,添加()到它,你得到


Functor()()


将导致_call_。


然后,从括号内删除那些''void'。他们就像疮。

唉!


V


2005-12- 07,Alf P. Steinbach写道:

* anderberg:

考虑以下代码,

#include< iostream>

struct Functor {
void operator()(void){std :: cout<< "函子" <<的std :: ENDL; }
''void''正式参数列表是C-ism;不要。




喜欢明白。

另外,应该是''const''。


当然,这不是关于代码''正确性'的问题。

只是一个简单的例子来说明我的问题。< br>

{
Functor();
返回0;



没有必要''main'',因为''main''是一个特殊功能。




见上文。

为什么不调用operator()来创建'?blockquote>

为什么要这样?




Functor a;

a();


我认为Functor()会转换成类似于
$的东西b $ b以上。我想我错了。但问题仍然存在 - 为什么

不是吗?

尝试Functor()()。




谢谢!


-

anderberg:人们只是突变。有些人比其他人差。


Consider the following code,

#include <iostream>

struct Functor {
void operator()(void) { std::cout << "Functor" << std::endl; }
};

int
main(void)
{
Functor();
return 0;
}

Why isn''t operator() called for the temporary object that''s created?

--
anderberg: People are just mutations. Some worse than others.

解决方案

* anderberg:

Consider the following code,

#include <iostream>

struct Functor {
void operator()(void) { std::cout << "Functor" << std::endl; }
''void'' as formal argument list is C-ism; don''t.

Also, should probably be ''const''.

};

int
main(void)
''void'' as formal argument list is C-ism; don''t.

{
Functor();
return 0;
Not necessary in ''main'', because ''main'' is a special function.

}

Why isn''t operator() called for the temporary object that''s created?



Why should it?

Try Functor()().

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


anderberg wrote:

Consider the following code,

#include <iostream>

struct Functor {
void operator()(void) { std::cout << "Functor" << std::endl; }
};

int
main(void)
{
Functor();
return 0;
}

Why isn''t operator() called for the temporary object that''s created?



Because you didn''t actually *call* it.

Functor()

is just creation of a temporary object. Now, add () to it and you get

Functor()()

which will result in a _call_.

And, drop those ''void'' from inside the parentheses. They''re like sores.
Ugh!

V


On 2005-12-07, Alf P. Steinbach wrote:

* anderberg:

Consider the following code,

#include <iostream>

struct Functor {
void operator()(void) { std::cout << "Functor" << std::endl; }
''void'' as formal argument list is C-ism; don''t.



Like to be explicit.
Also, should probably be ''const''.
Of course, this wasn''t a question about the code''s "correctness".
Just a simple case of illustrating my question.

{
Functor();
return 0;



Not necessary in ''main'', because ''main'' is a special function.



See above.

Why isn''t operator() called for the temporary object that''s created?



Why should it?



Functor a;
a();

I figured that Functor() would translate to something similar to
the above. I guess I was wrong. But the question remains - why
doesn''t it?
Try Functor()().



Thanks!

--
anderberg: People are just mutations. Some worse than others.


这篇关于功能对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆