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++/70818] New: Multiple calls of virtual base class contructor (with braced initialization)


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70818

            Bug ID: 70818
           Summary: Multiple calls of virtual base class contructor (with
                    braced initialization)
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: trivial
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tcorbat at hsr dot ch
  Target Milestone: ---

I've encountered a surprising behavior of a resulting executable when compiling
the following code (g++ -std=c++11):

- main.cpp ---------------------------------------------
#include <iostream>

struct V {
  V(){std::cout << "V()\n";}
};

struct A : virtual V {
  A() : V{} {std::cout << "A()\n";}
};

struct B : A {
  B(): V{}, A{} {std::cout << "B()\n";}
};


int main(int argc, char **argv) {
  B b{};
}
--------------------------------------------------------

I expected each constructor being called once, since B is responsible for
constructing V and construction of V as base of A is omitted(C++ Standard
[class.base.init]/7). But surprisingly the output was as follows:
V()
V()
A()
B()


This behavior changes when I modify the B constructor to call the A constructor
with parentheses instead of braced initializers:
  B(): V{}, A() {std::cout << "B()\n";}

This results in the expected output:
V()
A()
B()


According to my understanding both versions should behave the same and the
former is a bug.

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