help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with functor in g++


From: Bernd Strieder
Subject: Re: Problem with functor in g++
Date: Mon, 23 Oct 2006 15:54:08 +0200
User-agent: KNode/0.10.1

Hello,

okronglis@hurco.com wrote:

> template <class output_functor>
> void do_loop(output_functor& o)
> {

> };
> 

> a_int.do_loop(output());
 
output() is a temporary default-constructed object, and do_loop expects
a non-const reference to an output object. Taking a non-const reference
of a temporary is formally not allowed, this is a FAQ. VC8 is probably
wrong to accept this. There are many occasions in the STL where
functors are passed by value instead of reference, most probably
because of that.

I think, in this case a compiler theoretically could find out that the
output object is almost const, it has no members to change. But still,
the rule had been introduced to avoid deep trouble due to lost changes
to temporary objects, which could be hellish to debug.

The following works:

...
  output o;
  a_int.do_loop(o);
...

That should do it as well, probably even better:

...
 template <class output_functor>
 void do_loop(output_functor o)
....

Regards,

Bernd Strieder


reply via email to

[Prev in Thread] Current Thread [Next in Thread]