C ++返回对象副本 [英] C++ returning an object copy

查看:213
本文介绍了C ++返回对象副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下代码:

  class MyObjectHolder {
public:
std :: vector< int> getMyObject()const {
return myObject;
}

private:
std :: vector< int> myObject;
};

在我的程序的某一点,我试图使用 getMyObject 方法并对检索到的对象只使用 const 方法:

  myObject.size(); 
int a = myObject.front();




  • 现在,代码,以便没有 std :: vector< int> 的副本?



    < blockquote>

    这是有可能的编译器确定我只使用 const 方法对检索的对象(让我们假设没有 mutable 在其后面发生的无意义),它不会制作任何副本的对象,并执行这些<$ c $相反, MyObjectHolder 私人成员上的 const


  • 如果是,如果我没有明确声明 const std :: vector< int> ;

  • 如果没有,原因是什么?去做这个?



解决方案


现在,编译器可能会优化这个代码,使得没有 std :: vector< int>


否,编译器不知道调用者将如何处理该对象,除非正在使用对使用对象的所有代码的全局优化(编译器通常不能对其使用做出假设;此外,如果对象从dll导出,它根本不能做任何假设)。


如果是,如果我没有明确声明const std :: vector myObject作为const?


否,无论如何从非const转换为const可能是隐式的。


如果没有,什么原因不这样做?在这种情况下,这种优化将难以实现/推断这是可能的和正确的在这里/ etc ...?


应该在 getMyObject()中进行选择化,但编译器不能确保调用者不会丢弃const。实际上,这是一个关于使用 const 的一个非常老的争论,通常我认为总是想到 const 作为程序员的东西,而不是编译器。


I wrote the following code:

class MyObjectHolder {
public:
    std::vector<int> getMyObject() const {
        return myObject;
    }

private:
    std::vector<int> myObject;
};

At some point of my program I attempt to use the getMyObject method and use only const methods on the retrieved object:

const std::vector<int> myObject = myObjectHolder.getMyObject();
myObject.size();
int a = myObject.front();

  • Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

    Is it somehow possible that the compiler determines that I'm only using the const methods on the retrieved object (and let's assume there is no mutable nonsense happening behind it) and it would not make any copies of the objects and perform these const operations on the private member of the MyObjectHolder instead?

  • If yes, would it be possible if I didn't explicitly declare the const std::vector<int> myObject as const?

  • If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it's possible and correct here / etc... ?

解决方案

Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?

No, the compiler doesn't know what callers will do with that object unless you are making use of global optimization over all code that uses the object (the compiler can't generally make assumptions about its use; moreover if object is exported from a dll it can't make any assumption at all).

If yes, would it be possible if I didn't explicitly declare the const std::vector myObject as const?

No, anyway the conversion from non-const to const could be implicit.

If no, what are the reasons not to do this? In which cases this optimization would be to hard to implement / deduce that it's possible and correct here / etc... ?

It's an optmiziation that should be done inside getMyObject() but the compiler can't be sure that callers won't cast away the const. Actually this is a very old debate about the use of const, usually I think it's more clear to always think about const as something for programmers and not for compilers.

这篇关于C ++返回对象副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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