Reputation: 14603
say I have this struct:
template <typename ...A>
struct A
{
void f(A&& ...args)
{
fwd(std::forward<A>(args)...);
}
template <typename ...B>
void g(B&& ...args)
{
fwd(std::forward<B>(args)...);
}
};
Will f
and g
both forward perfectly, if A... == B...
? IMO, they should, but I am just asking to be sure.
EDIT:
The reason for this question is the common lecture about how perfectly forwarding functions should always be template functions. Obviously this is not true for f
.
Upvotes: 1
Views: 101
Reputation: 16737
Yes, if A... == B...
, there is no difference in behaviour. However, the reason for the common advice about forwarding functions needing to be templated is that you would rather have the compiler deduce the types(as in the case of a function template) instead of you having to specify the correct types(as in the case of a class template). This difference is illustrated by the following snippet for a type X
(of course, they don't satisfy A... == B...
) :
X x;
A<X>::f(X());
A<X>::g(X());//here, f and g have the same behaviour
A<X>::f(x);//fails to compile, since A<X>::f expects an rvalue ref.
A<X>::g(x);//works as expected - Here, B... = X&, while A... = X.
A<const X&>::f(X());//oh noes! fwd gets a const X&.
A<const X&>::g(X());//forwards correctly as B... = X, even though A... = const X&.
Upvotes: 1
Reputation: 76240
Will
f
andg
both forward perfectly, ifA... == B...
?
Yes. I see no reason whatsoever for which they shouldn't.
Upvotes: 2