Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function objects

Reply
Thread Tools

Function objects

 
 
anderberg
Guest
Posts: n/a
 
      12-07-2005
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-07-2005
* 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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2005
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
 
Reply With Quote
 
anderberg
Guest
Posts: n/a
 
      12-07-2005
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.
 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-07-2005

anderberg wrote:

> 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.


void func();
void func(void);

In C these two mean different things. In C++ they are exactly
equivalent. In C++, void func() *is* explicit. It means that func takes
no parameters.

C++ programmers do not expect to see a redundant 'void' in the formal
parameter list any more than they expect to see a redundant 'auto' in
front of every local variable declararion.

Gavin Deane

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      12-07-2005
anderberg <> wrote:
> On 2005-12-07, Alf P. Steinbach wrote:
>> * anderberg:
>>> 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?


Functor() creates a temporary Functor object and default initializes it.
As others have suggested, Functor()() does what you want. To
illustrate, add a default constructor:

#include <iostream>

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

int main()
{
Functor();
Functor()();
}

/*
Output:

Functor
Functor
()

*/

>> Try Functor()().

>
> Thanks!


--
Marcus Kwok
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-07-2005
anderberg wrote:

>>> 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?


If you rewrite your class to:

struct Functor {};

Would you expect the compiler to complain about a missing operator() in the
line saying:

Functor();

? If not, why do you expect the operator() to be called?




 
Reply With Quote
 
anderberg
Guest
Posts: n/a
 
      12-07-2005
On 2005-12-07, wrote:
>
> void func();
> void func(void);
>
> In C these two mean different things. In C++ they are exactly
> equivalent. In C++, void func() *is* explicit. It means that func takes
> no parameters.
>
> C++ programmers do not expect to see a redundant 'void' in the formal
> parameter list any more than they expect to see a redundant 'auto' in
> front of every local variable declararion.


Point taken. I'll make a habit not to use it, then. Last six years
of embedded C has obviously left a legacy.

--
anderberg: People are just mutations. Some worse than others.
 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      12-07-2005
anderberg wrote:
> On 2005-12-07, wrote:
>
>>void func();
>>void func(void);
>>
>>In C these two mean different things. In C++ they are exactly
>>equivalent. In C++, void func() *is* explicit. It means that func takes
>>no parameters.
>>
>>C++ programmers do not expect to see a redundant 'void' in the formal
>>parameter list any more than they expect to see a redundant 'auto' in
>>front of every local variable declararion.

>
>
> Point taken. I'll make a habit not to use it, then. Last six years
> of embedded C has obviously left a legacy.


Not to mention that many of us still program in both languages, and
while *yes, we know* that they're not the same language, in practice
things tend to run together.

--
Mike Smith
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-07-2005
Alf P. Steinbach wrote:

> * anderberg:


> > {
> > Functor();
> > return 0;

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


While techinically true, it's not any kind of error to include the
return. I always include it. If nothing else, it signals to the
maintainer that the original programmer didn't just forget about the
return, that 0 was indeed what was meant.

Some compilers will flag it with a warning.

Many common coding standards mandate the use of explict returns in
main().



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Function objects vs. function pointers Protoman C++ 11 12-09-2005 10:49 AM
Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. Mike Larkin ASP .Net 1 05-23-2005 12:33 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Re: function objects instead of function pointers? Brian C++ 2 10-31-2003 01:45 PM



Advertisments