Use of rvalue reference members?

后端 未结 4 993
离开以前
离开以前 2021-01-30 06:52

I was wondering what use an rvalue reference member has

class A {
  // ...
  // Is this one useful?
  Foo &&f;
};

Does it have any bene

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 07:33

    I've seen one very motivating use case for rvalue reference data members, and it is in the C++0x draft:

    template
    tuple
    forward_as_tuple(Types&&... t) noexcept;
    

    Effects: Constructs a tuple of references to the arguments in t suitable for forwarding as arguments to a function. Because the result may contain references to temporary variables, a program shall ensure that the return value of this function does not outlive any of its arguments. (e.g., the program should typically not store the result in a named variable).

    Returns: tuple(std::forward(t)...)

    The tuple has rvalue reference data members when rvalues are used as arguments to forward_as_tuple, and otherwise has lvalue reference data members.

    I've found forward_as_tuple subsequently helpful when needing to catch variadic arguments, perfectly forward them packed as a tuple, and re-expand them later at the point of forwarding to a functor. I used forward_as_tuple in this style when implementing an enhanced version of tuple_cat proposed in LWG 1385:

    http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1385

提交回复
热议问题