English 中文(简体)
C++ 具有高价值参照的构造
原标题:C++ constructor with rvalue reference

Consider this class with three constructors:

class Circle {

public:
 Circle(int r) {
      _radius = r;
 }

Circle(const Circle& c){
    _radius = c.radius();
    cout << endl << "Copy constructor with lvalue reference. Radius: " << _radius;
}

Circle(Circle&& c){
    _radius = c.radius();
    cout << endl << "Copy constructor with rvalue reference. Radius:" << _radius;
}

int radius() const {
    return _radius;
}

private:
    int _radius;
};

int main() {
     Circle c1(2);
     Circle c2(c1);
     cout << endl << c2.radius(); 
     Circle c3(Circle(4));
     cout << endl << c3.radius(); 
     return 0;
 }

“g++-std=c++0x” 产出如下:

Copy constructor with lvalue reference. Radius: 2
2
4

OK. The right constructors for the first two cases are called. But for the third case i.e., Circle c3(Circle(4)), I d expect the third constructor, (copy constructor with rvalue referecne) to be called but it s not the case. Obviously some constructor is called since c3 is properly instantiated but I don t understand why the compiler is not using the explicitly provided one. Am I missing something here?

最佳回答

为了达到高价值参照标准,它应当是非最根本的,因为建筑商论点的内容将有所改变,而且通常是一种改变歌剧状态的行动(尽管在你的具体情况下不是):

Circle(Circle&& c){ }

Also, you are seeing a copy elision here:

Circle c3(Circle(4));

so the move constructor doesn t get invoked. This is a standard compiler optimization that may or may not happen. If you were to construct a Circle like this:

Circle c3(std::move(c1));

then you would invoke the move constructor.

问题回答

No move constructors are being called because your compiler is too smart for your code ;)

 Circle c1(2);

这只是用<代码>int的换算构造构造构造构造物。

 Circle c2(c1);

这是一份复印件。 <代码>c1是一升值,因此它引发复制件。

 Circle c3(Circle(4));

Here, your compiler recognizes that you re basically telling it to construct the object twice. So it elides one of the object constructors. This is allowed in this instance by the C++ specification.

如果您的汇编者 could开建筑工程,那么它将进行搬迁。 另外,如果贵编者可以 t到,,将其 away弃。

因此,没有进展。

New compilers have a feature called RVO (Return value optimization) and try to avoid useless copies. In g++ if you put -fno-elide-constructors flag in your compile command it will avoid it. Nevertheless in your case even with this suppressing compiler flag, it will keep elide copy constructors and move constructors. If you want to see how your r-value constructor will be called use it as follow and compile it with above flag:

Circle MakeACircle(int r) {
      Circle temp(r);
      return temp;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签