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()
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.
Looks like a regression then.
I think this is probably caused by http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=175674 change.
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.
We regressed with r175639, the implementation of DR 990: http://gcc.gnu.org/ml/gcc-cvs/2011-06/msg01130.html
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.
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
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
Fixed.
Let's reopen this: testcase in Comment #1 isn't fixed yet.
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
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
Testcase from comment #1 fixed as well.