I have this code: (live eg: https://godbolt.org/z/js1qK9hd1)
struct big_class
{
std::string s1;
std::string s2;
std::string s3;
std::string s4;
std::string s5;
std::string s6;
std::string s7;
};
void func(const big_class &bc)
{
std::cout << bc.s1 << bc.s2 << bc.s3 << bc.s4 << bc.s5 << bc.s6 << bc.s7 << std::endl;
}
void fwd_func(big_class &&bc)
{
func(std::forward<big_class>(bc));
}
template<typename T>
void prefect_fwd_func(T &&bc)
{
func(std::forward<T>(bc));
}
int main()
{
big_class bc{"1", "2", "3", "4", "5", "6", "7"};
std::cout << "func" << std::endl;
func(bc);
std::cout << "fwd_func" << std::endl;
fwd_func(bc);
std::cout << "perfect_fwd_func" << std::endl;
prefect_fwd_func(bc);
}
So here fwd_func does not work - but in my mind its basically the same as the template perfect_fwd_func - only its not a template.
Why does this not work? - it says it can't bind lval to rval. I think its something to do with the template T &&
decaying into a T
or something, but still can't quite puzzle it together... What is the difference between the two?
If I wanted to just pass a big_class
type around - then I assume my best bet is to just pass it by const reference...