移动构造函数未按预期调用
move constructor not being called as expected
我是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
}
- 在c++中使用向量时,如何调用构造函数和析构函数
- C++:考虑但不调用构造函数的特殊性
- 对象实例化调用构造函数的次数太多
- 我使用向量来创建类对象列表.初始化向量时如何使用参数调用构造函数?
- C ++:通过大括号调用构造函数?
- 不能调用构造函数
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- 在模板化类的构造函数中调用构造函数
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 静态 std::map instatiation 在类的方法中调用构造函数吗?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- 我不明白在这个例子中什么时候调用构造函数
- 调用c++构造函数的不同方法
- 调用构造函数与将内联常量定义为默认参数
- 如何通过 Rust FFI 调用C++构造函数?
- "new"运算符是否总是调用构造函数?
- 无法调用构造函数
- 使用 "()" 调用构造函数不同于"{}"
- 确定是调用构造函数还是强制转换运算符的因素