Bug 91499 - Compile error when trying to aggregate-initialize a member array of non-movable objects with user-defined destructor
Summary: Compile error when trying to aggregate-initialize a member array of non-movab...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-08-20 09:57 UTC by Taras
Modified: 2020-01-29 11:13 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-01-29 00:00:00


Attachments
Output of "gcc -v" and the compilation error (1.11 KB, text/plain)
2019-08-20 09:57 UTC, Taras
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Taras 2019-08-20 09:57:56 UTC
Created attachment 46733 [details]
Output of "gcc -v" and the compilation error

This bug report is based on the stackoverflow discussion that can be found here: https://stackoverflow.com/questions/57559634/initialization-of-an-array-of-non-moveable-objects-why-does-such-code-fail-to-c

Here's the code that fails to compile on GCC:


class Test // non-copyable and non-movable class with virtual functions
{
public:
    Test() = delete;

    Test(const Test&) = delete;
    Test(Test&&) = delete;
    Test& operator=(const Test&) = delete;
    Test& operator=(Test&&) = delete;

    Test(int a, int b) : a_(a), b_(b) {}
    virtual ~Test() {}

    int a_;
    int b_;
};

//----------------

class B
{
public:
/*(1)*/ B() : test_{{1, 2}, {3, 4}} {} // Does not compile on GCC

private:
        Test test_[2];
};

//---------------- 

int main()
{
    B b;
    Test test[2] = {{1, 2}, {3, 4}}; // Successfully compiles
}


GCC 9.2 outputs:

<source>: In constructor 'B::B()':
<source>:23:35: error: use of deleted function 'Test::Test(Test&&)'
   23 | /*(1)*/ B() : test_{{1, 2}, {3, 4}} {} // Does not compile on GCC
      |                                   ^
<source>:7:5: note: declared here
    7 |     Test(Test&&) = delete;
      |     ^~~~

Other related information (the system type, the options given when GCC was configured/built, the complete command line that triggers the bug, the compiler output) is contained in the attached text file. Basically, this output was taken from the https://godbolt.org/ online compiler. But all other compilers that I've tried give the same error.
A preprocessed file (*.i*) is NOT included because I have "reduced the testcase to a small file that doesn't include any other file" (item "ii" from Detailed bug reporting instructions).

As explained by user L. F. on stackoverflow, this code is expected to be compiled successfully without requiring a move constructor.
Comment 1 Taras 2019-08-20 11:08:25 UTC
(In reply to Taras from comment #0)
> 
> Here's the code that fails to compile on GCC:
> 
> class Test // non-copyable and non-movable class with virtual functions
> {

upd.:

The destructor isn't necessarily required to be virtual in this example -- the same error occurs without the "virtual" keyword, as long as Test's destructor has a user-defined body:
  ~Test() {}
    ^ does NOT compile as well

but:
  ~Test() = default; // or if there's no explicitly mentioned destructor at all
    ^ compiles successfully

however:
  virtual ~Test() = default;
    ^ does NOT compile
Comment 2 Jonathan Wakely 2019-11-06 20:05:33 UTC
Another dup of PR 63707 ?