Kenshin wrote:
<snip>
template <typename Functor, typename Prmr1>
<deductable return type> forward(Functor f, Prmr1 const & p1)
{
return f(p1);
}
template <typename Functor, typename Prmr1, typename Prmr2>
<deductable return type> forward(Functor f, Prmr1 const & p1,
Prmr2 const & p2)
{
return f(p1, p2);
}
//And so forth for 3, 4 , 5... arguments.
Then the caller could call forward() as-is for pass-by-value or
pass-by-const-reference parameters, but use boost::ref() to pass by
non-const reference.
The only problem that I can see is trying to pass a function as an argument.
Thanks,
Andy.
AFAIK, boost::ref returns an implicit reference to an object passed by
value; it does not cast const away. Using boost::ref here will create
a "const reference_wrapper<Prmr1>(p1) &" object, & pass that on to
"f". If function "f" takes arguments by value or non-const ref, this
will not compile.
reference_wrapper<T> has a conversion operator that returns a non-const
reference.
operator T& () const;
Note the const qualifier.
That means that if the forwarding function's parameter is
reference_wrapper<T> const & and if f accepts T& then passing this
parameter as an argument to f has the desired effect of passing the
non-const reference to T.
Since, as I said before, the caller would only use boost::ref for
non-const ref parameters, I think this technique should work. I think
using boost::ref for pass-by-value should work too, but that's not required.
{ edits: quoted sig and banner removed. don't quote sigs or the banner. -mod }
[ comp.lang.c++.moderated. First time posters: Do this! ]