草庐IT

c++ - 在 C++0x 中未调用移动构造函数

coder 2024-02-10 原文

请在下面找到我的代码,我曾经调用移动构造函数(代码灵感来自其他站点)并让我知道它有什么问题,我使用的是 GCC 4.5.3

#include <iostream>
#include <vector>

class Int_Smart_Pointer {

  int *m_p;

public:
  Int_Smart_Pointer() {
    std::cout<<"Derfault Constructor"<< std::endl;
    m_p = NULL;
  }

  explicit Int_Smart_Pointer(int n) {
    std::cout<<"Explicit Constructor: " << n <<std::endl;
    m_p = new int(n);
  }

  Int_Smart_Pointer(const Int_Smart_Pointer& other) {
    std::cout<<"Copy Constructor: "<<std::endl;
    if(other.m_p)
      m_p = new int(*other.m_p);
    else
      m_p = NULL;
  }

  Int_Smart_Pointer(Int_Smart_Pointer&& other) {
    std::cout<<"Move Constructor: "<<std::endl;
    m_p = other.m_p;
    other.m_p = NULL;
  }

  Int_Smart_Pointer& operator=(const Int_Smart_Pointer& other) {
    std::cout<<"Copy Assignment"<< std::endl;
    if(this != &other) {
         delete m_p;
         if(other.m_p)
           m_p = new int(*other.m_p);
         else
           m_p = NULL;
    }

      return *this;
  }

  Int_Smart_Pointer& operator=(Int_Smart_Pointer&& other) {
    std::cout<<"Move Assignment"<< std::endl;
    if(this != &other) {
      delete m_p;
      m_p = other.m_p;
      other.m_p = NULL;
    }

      return *this;
  }

  ~Int_Smart_Pointer() {
    std::cout<<"Default Destructor"<< std::endl;
    delete m_p;
  }

  int get() const{
    if(m_p)
      return *m_p;
    else
      return 0;
  }

};

Int_Smart_Pointer square(const Int_Smart_Pointer& r) {
    const int i = r.get();
    return Int_Smart_Pointer(i * i);
}

Int_Smart_Pointer getMove_Constructor() {
  return Int_Smart_Pointer(100);
}


int main()
{
  Int_Smart_Pointer a(10);
  Int_Smart_Pointer b(a);
  b = square(a);

  std::cout<< std::endl;

  Int_Smart_Pointer c(30);
  Int_Smart_Pointer d(square(c)); //Move constructor Should be called (rvalue)

  std::cout<< std::endl;

  Int_Smart_Pointer e(getMove_Constructor()); //Move constructor Should be called (rvalue)


  std::cout<< std::endl;

  std::vector<Int_Smart_Pointer> ptr_vec;
  ptr_vec.push_back(getMove_Constructor());
  ptr_vec.push_back(getMove_Constructor());

  std::cout<< std::endl;

  return 0;
}

输出是

Explicit Constructor: 10
Copy Constructor:
Explicit Constructor: 100
Move Assignment
Default Destructor

Explicit Constructor: 30
Explicit Constructor: 900

Explicit Constructor: 100

Explicit Constructor: 100
Move Constructor:
Default Destructor
Explicit Constructor: 100
Move Constructor:
Move Constructor:
Default Destructor
Default Destructor

Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor
Default Destructor

当我们在构造时使用 std::move 时,它​​正在调用移动构造函数。

Int_Smart_Pointer d(std::move(square(c))); //used std::move and Move constructor called
Int_Smart_Pointer e(std::move(getMove_Constructor())); //used std::move works as above

但是即使我们不使用,gerMove_Constructor 和 square 返回值在构造对象时也会变成右值,因为我们找不到地址空间或对它们的引用,

如果我的理解有问题,请告诉我,如果没有,那么为什么不调用移动构造函数。

提前致谢。 萨提亚

最佳答案

当按值返回局部变量时,编译器仍然允许像 C++03 中那样省略复制构造函数(返回值优化)。

关于c++ - 在 C++0x 中未调用移动构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7645985/

有关c++ - 在 C++0x 中未调用移动构造函数的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  7. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  8. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  9. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  10. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

随机推荐