Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&&

前端 未结 2 1571
难免孤独
难免孤独 2020-11-22 06:14

The following link provides the 4 forms of reference collapsing (if I\'m correct that these are the only 4 forms): http://thbecker.net/articles/rvalue_references/section_08.

2条回答
  •  我寻月下人不归
    2020-11-22 07:04

    The rules are actually pretty simple. Rvalue reference is a reference to some temporary value that does not persist beyond the expression that uses it - in contrast to lvalue reference which references persisting data. So if you have a reference to a persisting data, no matter what other references you combine it with, the actual referenced data is an lvalue - this covers for the first 3 rules. The 4th rule is natural as well - rvalue reference to rvalue reference is still a reference to non-persistent data, hence rvalue reference is yielded.

    Yes, the C++11 utilities rely on these rules, implementation provided by your link matches the real headers: http://en.cppreference.com/w/cpp/utility/forward

    And yes, the collapsing rules along with template argument deduction rule are being applied when using std::move and std::forward utilities, just like explained in your link.

    The usage of type traits such as remove_reference is really depends on your needs; move and forward cover for the most casual cases.

提交回复
热议问题