This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/54325] New: C++11 uniform initialization syntax for argument-less abstract base class constructor fails


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54325

             Bug #: 54325
           Summary: C++11 uniform initialization syntax for argument-less
                    abstract base class constructor fails
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: moritz@bunkus.org


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()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]