[fusion] clarification on stateful unary function objects
Hi there,
While trying to use a stateful unary function objects, I stumbled over
and old discussion from 2007 [1]. From the current Fusion documentation,
I understand that the functor passed to the for_each algorithm must
satisfy the Regular Callable Object concept. I presume that regularity,
which I understand as replacing inputs with equal objects results in
equal outputs, here implies that operator()(T& x) must be const. In
other words, mutable function objects are not supported. As pointed out
in [1], it is of course possible to bypass this restriction by defining
the state that needs to changed as mutable.
I was wondering if this is still the correct way to deal with mutable
function objects, as illustrated in the small example below:
#include <iostream>
#include <string>
#include
AMDG Matthias Vallentin wrote:
While trying to use a stateful unary function objects, I stumbled over and old discussion from 2007 [1]. From the current Fusion documentation, I understand that the functor passed to the for_each algorithm must satisfy the Regular Callable Object concept. I presume that regularity, which I understand as replacing inputs with equal objects results in equal outputs, here implies that operator()(T& x) must be const. In other words, mutable function objects are not supported. As pointed out in [1], it is of course possible to bypass this restriction by defining the state that needs to changed as mutable.
I was wondering if this is still the correct way to deal with mutable function objects, as illustrated in the small example below:
It might work, but I don't advise it. Generally mutable state should be stored outside the function object, (the function object can contain a pointer or reference to it). In Christ, Steven Watanabe
On Sun, Feb 28, 2010 at 07:57:38PM -0800, Steven Watanabe wrote:
Generally mutable state should be stored outside the function object, (the function object can contain a pointer or reference to it).
Thanks for the tip, Steven. Matthias -- Matthias Vallentin vallentin@icsi.berkeley.edu http://www.icir.org/matthias
participants (2)
-
Matthias Vallentin
-
Steven Watanabe