Hacker News new | past | comments | ask | show | jobs | submit login
Forwarding References in C++ (lemire.me)
4 points by mfiguiere 26 days ago | hide | past | favorite | 1 comment



Like most things in C++, forwarding references are one of these things that seems to really confuse people. This article seems to think it's another magical rule implemented by the compiler only for templated contexts, but it's really just a consequence of the reference collapsing rules that apply everywhere.

  template <typename T> void foo(T &&t);
  Bar bar;
  // Template params are inferred as follows
  foo<Bar>(Bar{});
  foo<Bar&>(bar);
Similarly to how a const (const Foo) collapses to const Foo, a (Foo&)&& is considered simply a Foo&. The magic then isn't with the && or the templated context itself, but rather the template parameter type inference which determines the correct qualification which makes the call possible.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: