Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array-member-variable construction using move constructor in GCC <= 9.3 but GCC > 9.3

With GCC++ <= 9.3 the code below has compile error about using move constructor which is being deleted:

<source>: In constructor 'Widget::Widget()':
<source>:16:24: error: use of deleted function 'Child::Child(Child&&)'
   16 |     Widget() : m_array{} {}
      |                        ^
<source>:11:5: note: declared here
   11 |     Child(Child&&) = delete;
      |     ^~~~~

With GCC++ >9.3 the error won’t be seen anymore.
Is it something about compiler specific issue or the code itself ?
As I comment in the code if the destructor is commented out the error won’t be seen.

https://godbolt.org/z/4s3o1sxPs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Base {
public:
    Base() {}
    virtual ~Base() = default;//comment out error won't bee seen anymore
    Base(Base&&) = delete;
};

class Child  : public Base{
public:
    Child() = default;
    Child(Child&&) = delete;
};

class Widget {
public:
    Widget() : m_array{} {}
private:
    Child m_array[2];
};

int main()
{
    Widget wd{};
}

>Solution :

Given that the behavior changed between gcc 9.3 and 9.4; this looks like a compiler bug that got fixed.

In fact, looking at the list of C++ compiler bugs fixed in gcc 9.4, it’s not hard to find https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63707 "Brace initialization of array sometimes fails if no copy constructor" which seems to be the bug you encountered.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading