Reputation: 324
I have the following class which stores a perfectly forwarded reference to a function:
template <class F>
class Visitor
{
public:
Visitor(F &&f) : _f(std::forward<F>(f))
{
}
xt::LOOP_CONTROL OnElem(xvi::Elem &elem)
{
return _f(elem);
// or should it be:
// return std::forward<F>(_f)(elem);
}
private:
F &&_f;
};
Wondering if it makes a difference to call _f(elem)
or std::forward<F>(_f)(elem);
?
Upvotes: 0
Views: 178
Reputation: 218238
Lambda cannot have ref qualified operator ()
currently, so for them, both forms are equivalent.
For custom functor, it might differ with std::forward<F>(_f)(elem);
:
struct MyFunctor
{
xt::LOOP_CONTROL operator()(xvi::Elem &elem) & { std::cout << "lvalue"; return {};}
xt::LOOP_CONTROL operator()(xvi::Elem &elem) && { std::cout << "rvalue"; return {};}
};
xvi::Elem elem;
Visitor<MyFunctor>(MyFunctor{}).OnElem(elem); // rvalue
MyFunctor func;
Visitor<MyFunctor&>(func).OnElem(elem); // lvalue
Upvotes: 2