Re: function object for [] and *
On Jun 25, 12:20 pm, gpderetta <gpdere...@gmail.com> wrote:
On Jun 25, 9:46 am, James Kanze <james.ka...@gmail.com> wrote:
[...]
Boost's bind definitely belongs in everyone's tool kit, but it
has it's limits as well---if the operator[] in question is the
built in version, for example, it won't work. The next version
of the standard will also include some support for lambda
expressions, which would be an even more general solution.
It will also not work if operator[] has been overloaded for const and
non const 'this' (as it is usually the case). You need an explicit
(and ugly) cast then.
Good point.
The OP bind example also has a problem if used
with any container but plain arrays: bind closes its arguments by copy
so it will cause the container to be copied: use boost::ref or
boost::cref.
With boost lambda you can do:
namespace ll = boost::lambda;
std::transform(idx, idx+n, out, ll::ret<whatever>(ll::var(in)[_1]));
where 'whatever' is the result type for operator[]. In this case the
const qualified value_type of 'in' should be enough. For std::vector
(and standard containers in general) and builtin arrays, you do not
need 'ret<>':
namespace ll= boost::lambda;
std::vector<int> m = ...;
std::vector<int> idx = ...;
std::vector<int> out = ...;
std::transform(idx.begin(), idx.end(), out.begin(), ll::var(m)
[ll::_1]);
Which doesn't look that ugly.
No. I didn't consider boost::lambda, because it seems that
every time I try it on something real, I hit up against some
limitations of it, and end up having to add extra qualifiers,
etc., in order to make it work. Of course, your ll::var would
qualify there, but in this case, I guess the rest of the
expression is simple enough that it doesn't feel too bothersome.
In general, I think real support for lambdas (at the language
level) should open up a lot of possibilities along these lines.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34