To truly forward a call to a member function, one must consider the necessity of correctly forwarding the *this
value for the member call.
The following:
template<typename Type>
struct Fwd {
Type member;
template<typename ...Args>
decltype(auto) Func(Args&&... args)
noexcept(noexcept(member.Func(std::forward<Args>(args)...)))
{ return member.Func(std::forward<Args>(args)...); }
};
is sufficient for forwarding arguments and the exception specification, as you might have guessed. But it is insufficient to perfect forward *this
:
struct S {
// These overloads are reachable through Fwd<S>::Func().
void Func(int) & {}
void Func(int&&) & {}
// These other overloads are not.
void Func(int) const&;
void Func(int&&) const&;
void Func(int) volatile&;
void Func(int&&) volatile&;
void Func(int) const volatile&;
void Func(int&&) const volatile&;
void Func(int) &&;
void Func(int&&) &&;
// (These are rather uncommon, just provided for completude.)
void Func(int) const&&;
void Func(int&&) const&&;
void Func(int) volatile&&;
void Func(int&&) volatile&&;
void Func(int) const volatile&&;
void Func(int&&) const volatile&&;
};
There are two solutions for this problem. One is to simply create each and every other possible overload manually, possibly with a macro:
#define FWD(member, Func) \
template<typename ...Args> \
decltype(auto) Func(Args&&... args) & \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return member.Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) const& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return member.Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) volatile& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return member.Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) const volatile& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return member.Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) && \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return std::move(member).Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) const&& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return std::move(member).Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) volatile&& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return std::move(member).Func(std::forward<Args>(args)...); } \
\
template<typename ...Args> \
decltype(auto) Func(Args&&... args) const volatile&& \
noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
{ return std::move(member).Func(std::forward<Args>(args)...); }
template<typename Type>
struct Fwd {
Type member;
FWD(member, Func)
};
The other solution is to avoid a member function altogether and use a free function:
template<typename Fwd, typename ...Args>
decltype(auto) Func(Fwd&& fwd, Args&&... args)
noexcept(noexcept(std::forward<Fwd>(fwd).Func(std::forward<Args>(args)...))) {
return std::forward<Fwd>(fwd).Func(std::forward<Args>(args)...);
}