This is the mail archive of the gcc@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? Exceptions and implicit constructors.


Hi all,

Is this a bug in GCC or does the code below incorrectly use exceptions
and virtual inheritance?


I expect the code below to display:

Constructing child2.
Caught exception: 3

However it causes an abort after displaying the first line. Looking
further into this i found that when GCC creates the implicit default
constructor for the GrandChild class, it gives it a no-throw specifier:
throw()



Usually when GCC creates an implicit default constructor it gives it a
no-throw specifier if that class and all its parents only construct
primitive data without calling on user defined constructors. In the case
below GCC seems to ignore that it uses a user defined constructor
defined in Child2() I assume because of the virtual inheritance.

To work around the problem, it is a simple matter of defining a default
constructor in GandChild OR even adding an attribute to the GandChild
class that has complex construction like std::string. E.g. Change
GrandChild class to look like:

class GrandChild : public Child1, public Child2
{
public:
   std::string  s;
};

Thanks,
Brendon.





class Parent
{
public:
   int data;
};

class Child1 : public virtual Parent
{
};

class Child2 : public virtual Parent
{
public:
   Child2()
   {
      std::cout << "Constructing child2." << std::endl;
      throw 3;
   }
};

class GrandChild : public Child1, public Child2
{
};

int main()
{
   try
   {
      GrandChild c;
   }
   catch(int i)
   {
      std::cout << "Caught exception: " << i << std::endl;
   }
   return 0;
}


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