Bug 54325 - [4.7/4.8 Regression] C++11 uniform initialization syntax for argument-less abstract base class constructor fails
Summary: [4.7/4.8 Regression] C++11 uniform initialization syntax for argument-less ab...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.1
: P2 normal
Target Milestone: 4.7.3
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-08-19 18:29 UTC by Moritz Bunkus
Modified: 2013-01-02 21:05 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Bunkus 2012-08-19 18:29:00 UTC
Using {} syntax for base class initialization fails with "cannot allocate an object of abstract type 'Base'" for abstract base classes with argument-less constructors. I'll include two code samples. The first one does work with g++ 4.6.1 (Ubuntu 11.10), g++ 4.6.3 (Ubuntu 12.04) and clang 3.1 but does NOT work with g++ 4.7.0 (Ubuntu 12.04) and g++ 4.7.1 (self-compiled). The second one works with 4.7.0 and 4.7.1 as well.

Example code that does NOT work with 4.7.x but does with older versions/other compilers:

class Base {
public:
  Base() {};
  virtual ~Base() {};

  virtual void do_stuff() = 0;
};

class Derived: public Base {
public:
  Derived() : Base{} {};
  virtual ~Derived() {};

  virtual void do_stuff() {};
};

int
main() {
  Derived d;

  return 0;
}


Changing Base's constructor to take an additional argument lets it work suddenly:

class Base {
public:
  int m_dummy;

  Base(int dummy): m_dummy{dummy} {};
  virtual ~Base() {};

  virtual void do_stuff() = 0;
};

class Derived: public Base {
public:
  Derived() : Base{42}  {};
  virtual ~Derived() {};

  virtual void do_stuff() {};
};

int
main() {
  Derived d;

  return 0;
}

$ ~/opt/gcc/4.7.1/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/home/mosu/opt/gcc/4.7.1/bin/g++
COLLECT_LTO_WRAPPER=/home/mosu/opt/gcc/4.7.1/libexec/gcc/x86_64-unknown-linux-gnu/4.7.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --enable-checking=release --enable-languages=c,c++ --prefix=/home/mosu/opt/gcc/4.7.1
Thread model: posix
gcc version 4.7.1 (GCC)

Actual error message:

$ ~/opt/gcc/4.7.1/bin/g++ -std=c++11 -o fails fails.cpp
fails.cpp: In constructor ‘Derived::Derived()’:
fails.cpp:11:20: error: cannot allocate an object of abstract type ‘Base’
fails.cpp:1:7: note:   because the following virtual functions are pure within ‘Base’:
fails.cpp:6:16: note:   virtual void Base::do_stuff()
Comment 1 drtwox 2012-09-07 21:34:57 UTC
The failing example can be simplified somewhat.

class base
{
    protected:
        base()
        {}
};

class derived : public base
{
    public:
        derived()
            : base{} // <-- Note the c++11 curly brace syntax
        {}
};

int main()
{
    derived d1;
    return 0;
}

Output:
$ g++-4.7 -std=c++11 -Wall -Wextra -pedantic curly.cpp -o curly
curly.cpp: In constructor ‘derived::derived()’:
curly.cpp:4:13: error: ‘base::base()’ is protected
curly.cpp:19:24: error: within this context

Replace g++4.7 with g++4.6 or clang++3.1 and it compiles without warnings or errors.
Comment 2 Paolo Carlini 2012-10-01 16:56:21 UTC
Looks like a regression then.
Comment 3 Jakub Jelinek 2012-11-19 13:33:41 UTC
I think this is probably caused by http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175674 change.
Comment 4 Paolo Carlini 2012-11-20 19:08:21 UTC
I just tried and r175673 also rejects the reduced code in Comment #1. Let's see if I can find the exact revision where we regressed.
Comment 5 Paolo Carlini 2012-11-21 00:41:19 UTC
We regressed with r175639, the implementation of DR 990:

  http://gcc.gnu.org/ml/gcc-cvs/2011-06/msg01130.html
Comment 6 Paolo Carlini 2012-11-21 03:14:14 UTC
It seems that the build_value_init calls added by r175639 have issues because it doesn't have the information that the value initialization is happening from the default constructor of a derived type.
Comment 7 Jason Merrill 2012-12-07 04:54:42 UTC
Author: jason
Date: Fri Dec  7 04:54:27 2012
New Revision: 194284

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194284
Log:
	PR c++/54325
	* tree.c (build_aggr_init_expr): Don't check for abstract class.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist-pure.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/tree.c
    trunk/gcc/testsuite/g++.dg/other/abstract3.C
Comment 8 Jason Merrill 2012-12-07 05:13:42 UTC
Author: jason
Date: Fri Dec  7 05:13:33 2012
New Revision: 194290

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194290
Log:
	PR c++/54325
	* tree.c (build_aggr_init_expr): Don't check for abstract class.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/cpp0x/initlist-pure.C
Modified:
    branches/gcc-4_7-branch/gcc/cp/ChangeLog
    branches/gcc-4_7-branch/gcc/cp/tree.c
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/other/abstract3.C
Comment 9 Jason Merrill 2012-12-07 05:15:03 UTC
Fixed.
Comment 10 Paolo Carlini 2012-12-27 08:41:13 UTC
Let's reopen this: testcase in Comment #1 isn't fixed yet.
Comment 11 Jason Merrill 2013-01-02 20:54:46 UTC
Author: jason
Date: Wed Jan  2 20:54:42 2013
New Revision: 194820

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194820
Log:
	PR c++/54325
	* call.c (build_new_method_call_1): Don't use build_value_init for
	user-provided default constructors.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist-protected.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
Comment 12 Jason Merrill 2013-01-02 20:56:37 UTC
Author: jason
Date: Wed Jan  2 20:56:29 2013
New Revision: 194821

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=194821
Log:
	PR c++/54325
	* call.c (build_new_method_call_1): Don't use build_value_init for
	user-provided default constructors.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/cpp0x/initlist-protected.C
Modified:
    branches/gcc-4_7-branch/gcc/cp/ChangeLog
    branches/gcc-4_7-branch/gcc/cp/call.c
Comment 13 Jason Merrill 2013-01-02 21:05:46 UTC
Testcase from comment #1 fixed as well.