使用右值引用成员?

发布于 2024-10-13 21:47:13 字数 152 浏览 5 评论 0原文

我想知道右值引用成员有什么用处

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

与左值引用成员相比它有什么优点或缺点?它的主要用途是什么?

I was wondering what use an rvalue reference member has

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

Does it have any benefits or drawbacks compared to an lvalue reference member? What is a prime usecase of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

方觉久 2024-10-20 21:47:14

我看到了右值引用数据成员的一个非常令人兴奋的用例,它位于 C++0x 草案中:

template<class... Types>
tuple<Types&&...>
forward_as_tuple(Types&&... t) noexcept;

效果:构造一个元组
对 t 中参数的引用
适合作为参数转发
到一个函数。因为结果可能
包含对临时的引用
变量,程序应确保
该函数的返回值是
不会比它的任何论点更长久。
(例如,程序通常应该
不将结果存储在命名的
变量)。

返回:元组<类型&&...>(std::forward<类型>(t)...)

当右值用作forward_as_tuple 的参数时,元组具有右值引用数据成员,否则具有左值引用数据成员。

我发现,当需要捕获可变参数时,forward_as_tuple 随后很有帮助,将它们完美地转发为元组,并在转发到函子时重新扩展它们。在实现 LWG 1385 中提出的 tuple_cat 的增强版本时,我以这种风格使用了forward_as_tuple:

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

template<class... Types>
tuple<Types&&...>
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<Types&&...>(std::forward<Types>(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

微凉徒眸意 2024-10-20 21:47:14

根据 Stephan T. Lavavej,右值引用数据成员没有用。

[at 31:00] 我看到程序员在掌握右值引用时所做的事情是,他们开始变得有点疯狂,因为他们太强大了。他们开始说“哦,我将拥有右值引用数据成员,我将拥有右值引用局部变量,我将拥有右值引用返回值!”然后他们编写这样的代码:[...]

According to Stephan T. Lavavej, rvalue reference data members have no use.

[at 31:00] The thing I've seen programmers do when they get hold of rvalue references is that, they start to go a little crazy, because they're so powerful. They start saying "Oh, I'm gonna have rvalue reference data members, I'm gonna have rvalue reference local variables, I'm gonna have rvalue reference return values!" And then they write code like this: [...]

错爱 2024-10-20 21:47:14

只是在这里大声思考,但它在函子中没有用处吗?构造函数通常用于“柯里化”,在实际函数调用之前提前绑定一些参数。

因此,在这种情况下,类成员只是即将到来的函数调用的临时场所(或手动实现的闭包),并且我认为右值引用没有任何意义。

但在“常规”非函子类中,我看不出有什么意义。

Just thinking out loud here, but wouldn't it have a use in functors? The constructor is often used for "currying", binding some parameters in advance, before the actual function call.

So in this context, the class member is just a staging ground (or a manually implemented closure) for the upcoming function call, and I see no reason why a rvalue reference wouldn't be meaningful there.

But in "regular" non-functor classes, I don't see much point.

守护在此方 2024-10-20 21:47:14
class A {
  // ...
  // Is this one useful?
  Foo &&f; 
};

在这种特定情况下,没有理由使用右值引用。它不会给你带来任何你以前做不到的事情。

但您可能想用参数化类型定义数据成员。例如,std::tuple 将支持左值和右值引用数据成员。这样,您就可以对表达式的值类别进行编码,这对于“延迟完美转发”可能会派上用场。标准草案甚至包括形式的函数模板

template<class Args...>
tuple<Args&&...> pack_arguments(Args&&...args);

但老实说我不确定它的用处。

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

In this specific case, there is no reason to use an rvalue reference. It doesn't buy you anything you couldn't have done before.

But you may want to define data members with parameterized types. std::tuple is going to support lvalue and rvalue reference data members, for example. This way it allows you to codify an expression's value category which might come in handy for "delayed perfect forwarding". The standard draft even includes a function template of the form

template<class Args...>
tuple<Args&&...> pack_arguments(Args&&...args);

But I'm honestly not sure about its usefulness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文