I have a struct F
with a function foo
that has different implementation whether F
is a temporary or not
struct F{
void foo() & { std::cout << "F::foo() &" << std::endl; }
void foo() && { std::cout << "F::foo() &&" << std::endl; }
};
Another struct A
has a copy of F
and in its function bar
calls F::foo
. I want to use the correct version of F::foo()
. Therefore the implementation is:
struct A{
void bar() & {
f.foo();
}
void bar() && {
std::move(f).foo();
}
F f;
};
I'm wondering if I really have to provide two implementations of A::bar()
. Isn't there a smart way to use std::forward
to automatically decide which F::foo()
should be used?
An attempt:
struct B{
void bar() {
std::forward<F>(f).foo();
}
F f;
};
However, this does not work. It calls F::foo() &&
every time.
friend
based solution, too, but ended up with something less elegent. Nice! – Lundberg