are these functions equivalent?
template <class T>
void foo(T && t)
{
bar(std::forward<T>(t));
}
template <class T>
void foo2(T && t)
{
bar(std::forward<decltype(t)>(t));
}
template <class T>
void foo3(T && t)
{
bar(std::forward(t));
}
if they are, can I always use this macro for perfect forwarding?
#define MY_FORWARD(var) std::forward<decltype(var)>(var)
or just use
bar(std::forward(t));
I believe foo2
and foo3
are same, but I found people are always use forward like foo
, is any reason to explicitly write the type?
I understand that T
and T&&
are two different types, but I think std::forward<T>
and std::forward<T&&>
always give the same result?
Edit:
the reason I want to use macro is I want to save some typing on following C++1y code, I have many similar code in different places
#define XLC_FORWARD_CAPTURE(var) var(std::forward<decltype(var)>(var))
#define XLC_MOVE_CAPTURE(var) var(std::move(var))
template <class T, class U>
auto foo(T && func, U && para )
{
auto val = // some calculation
return [XLC_FORWARD_CAPTURE(func),
XLC_FORWARD_CAPTURE(para),
XLC_MOVE_CAPTURE(val)](){
// some code use val
func(std::forward<U>(para));
};
}
foo3
you added isn't valid.