移动构造函数未按预期调用

move constructor not being called as expected

本文关键字:调用 构造函数 移动      更新时间:2023-10-16

我是C 0x的新手,我正在尝试将头缠绕在rvalue参考文献和移动构造函数上。我使用的是g 4.4.6,带有-std = c 0x,我对以下代码感到困惑:


    class Foo 
    {
    public:
      Foo() 
        : p( new int(0) )
      {
        printf("default ctorn");
      }
      Foo( int i )
        : p( new int(i) )
      {
        printf("int ctorn");
      }
      ~Foo() 
      {
        delete p;
        printf("destructorn");
      }
      Foo( const Foo& other ) 
        : p( new int( other.value() ) )
      {
        printf("copy ctorn");
      }

      Foo( Foo&& other )
        : p( other.p )
      {
        printf("move ctorn");
        other.p = NULL;
      }
      int value() const 
      {
        return *p;
      }
    private:
      // make sure these don't get called by mistake
      Foo& operator=( const Foo& );
      Foo& operator=( Foo&& );
      int* p;
    };

    Foo make_foo(int i) 
    {
      // create two local objects and conditionally return one or the other
      // to prevent RVO
      Foo tmp1(i);
      Foo tmp2(i);
      // With std::move, it does indeed use the move constructor
      //  return i ? std::move(tmp1) : std::move(tmp2);
      return i ? tmp1 : tmp2;
    }

    int main(void) 
    {
      Foo f = make_foo( 3 );
      printf("f.i is %dn", f.value());
      return 0;
    }

我发现,作为书面形式,编译器使用复制构造函数在main()中构建对象。当我使用std :: Move_foo()中的移动线时,则在main()中使用移动构造函数。为什么STD ::在Make_foo()内部有必要的移动?我认为,尽管tmp1和tmp2在make_foo()内被命名为对象,但是当它们从函数返回时,它们应该成为临时性。

这是您的问题:

return i ? tmp1 : tmp2;

如果返回语句仅return var;,则函数中的本地变量将仅在返回语句中移动。如果您想进行该测试,则需要使用IF:

if (i) {
   return tmp1;
} else {
   return tmp2;
}

引用有点复杂,但在12.8/31和12.8/32

12.8/32当满足副本操作的省略标准或将被满足时,除了源对象是函数参数的事实,并且要复制的对象由lvalue指定,超载分辨率可以选择副本的构造函数首先执行,好像对象是由rvalue指定的[...]

即使表达式是lvalue,也将在满足12.8/31中的标准时被认为是rvalue,该块中的第二个选项是:

12.8/31在带有类返回类型的函数中的返回语句中,当表达式是具有相同CV UNQUALIFIED类型的非挥发性自动对象的名称(函数或catch-clause子句参数除外)作为函数返回类型,可以通过将自动对象直接构造到函数的返回值中来省略复制/移动操作。

确定return tmp;允许复制elision的哪个,但return (cond?tmp:tmp);不。

请注意,要使编译器在返回语句中生成隐式std::move,返回对象必须是elision的候选对象,除非它也是该函数的参数。使用条件操作会抑制复制责任,同时抑制编译器从对象移出。第二种情况可能更简单地编码:

Foo make_foo(Foo src) {
   return src;           // Copy cannot be elided as 'src' is an argument
}