RVO替代方案 [英] RVO alternative

查看:70
本文介绍了RVO替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我遇到动态内存问题,并从函数返回值。考虑以下函数签名:

Hi,

I''m having problems with dynamic memory, and returning values from functions. Consider the following function signature:

展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号

推荐答案

你的问题没有意义。一方面,您会询问动态内存分配,这是C ++的一部分。然后你谈谈RVO,这是一个与你编写的代码无关的编译器优化。


你不能直接在堆栈上动态使用allocate。示例对于堆栈来说分配太多,或者直到运行时才知道。
Your question doesn''t really make sense. On one hand you ask about dynamic memory allocation, which is part of C++. Then you talk about RVO, which is a compiler optimization that has nothing to do with the code you write.

You use allocate dynamically when you can''t directly on the stack. Examples are too much to allocate for the stack, or you don''t know until runtime.


感谢您的回答。我试图用我的例子展示的是我经常想把东西放在调用者堆栈上。在许多情况下都是如此,其中方法构建,初始化并返回实例。我展示的示例是调用者未知实现类的示例,我认为这是一个重要的示例,因为它禁止调用者在调用构建器方法之前在其堆栈上实例化变量(这种独立性是我想要的经常实现)。


为了使对象能够存活到父作用域(将它放在调用者堆栈上),似乎需要按值返回变量。这通常意味着在复制回调用者堆栈时会产生额外的复制开销。我想知道复制语义背后的基本原理是什么。这似乎是低效的,并且将构造到呼叫者堆栈上并不太难。关键词或成语(很像RVO的作品)。可能存在一些硬件考虑因素或其他使复制语义成为必需的问题。我不明白为什么没有办法明确要求RVO行为(或者我根本不理解如何编写它)。
Thank you for your answer. What I tried to show with my example is that I often want to put things on the caller stack. This is true on many occasions, where methods build, initializes and returns an instance. The example I showed was an example where the implementation class is unknown by the caller, which I felt was an important example, since it prohibits the caller to instantiate the variable on his stack before calling the builder method (this independence is what I want to achieve quite often).

In order to make an object survive onto the parent scope (put it on the callers stack), it seems as if you need to return variables by value. This generally means extra copy overhead, when copying back to the callers stack. I would like to know what the rationale behind the copy semantics is. It seems inefficient, and it would not be too hard to have a "construct onto callers stack" keyword or idiom (much like RVO works). Maybe there are some hardware considerations, or other issues which makes copy semantics necessary. I don''t understand why there is no way of explicitly ask for RVO behavior (or I simply don''t understand how to write it).


作为C ++中的一般规则避免堆栈内存就像黑死病一样。


将指针指向堆栈项是你在C ++程序中可以做的最危险的事情。当你需要它们时,堆栈项已超出范围。


而是使用堆。 new运算符返回指向分配的指针。我建议你学习如何使用带指针的句柄。你可能会读到这个:
http://bytes.com/forum/thread651599.html


Beyind,返回的语义是返回引用不会复制。否则,会复制一个副本,这是一个复制构造函数调用,通常是一个深层复制。所以不要这样做。另一方面,如果引用引用本地堆栈变量,则返回引用是没用的。所以也不要这样做。否则引用是一个函数参数,它也是一个引用。返回任何东西都没有意义。这使得基于取消引用堆指针返回引用。所以也不要那样。只需返回指针,呃,手柄。
As a general rule in C++ avoid stack memory like the Black Death.

Passing pointers to stack items is the single most dangerous thing you can do in a C++ program. Right when you need them, the stack item has gone out of scope.

Instead, use the heap. The new operator returns a pointer to the allocation. I recommend you learn how to use a handle with pointers. You might read this:
http://bytes.com/forum/thread651599.html.

Beyind that, the return emantics are that returning a reference does not make a copy. Otherwise a copy is made and this is a copy constructor call which is usually a deep-copy. So don''t do this. On the other hand, returning a reference is useless if it refers to a local stack variable.So don''t do that either. Otherwise the reference is to a function argument that is also a reference. So there''s no point in returning anything. That leaves returning a reference based on dereferencing a heap pointer. So don''t to that either. Just return the pointer, er, handle.


这篇关于RVO替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆