The C++ standard allows to add exceptions to the list of throwable exceptions
in virtual functions. But this does not work with multiple inheritence, as the
following example shows:

// CODE BEGIN

#include <string>
#include <vector>
#include <iostream>
#include <exception>


class my_exception
{
protected :
        std::string error_message_;

public :
        my_exception(std::string const & error_message)
:error_message_(error_message) {}
        virtual ~my_exception() throw() {}

        std::string const & what() const throw() {return error_message_;}
};


template <class T>
class A
{
protected :
        T obj_;

public :
        A(T const & obj) :obj_(obj) {}
        virtual ~A() {}

        virtual std::string get_obj_type() const =0;
};


class B : public A<std::string>
{
public :
        B(std::string const & obj) :A<std::string>(obj) {}
        virtual ~B() {}

        virtual std::string get_obj_type() const throw(std::exception) {return
"string";}
};


class C : public A<std::vector<int> >
{
public :
        C(std::vector<int> const & obj) :A<std::vector<int> >(obj) {}
        virtual ~C() {}

        virtual std::string get_obj_type() const throw(std::exception) {return
"vector<int>";}
};


class D : public B, public C
{
public :
        D(std::string const & obj1, std::vector<int> const & obj2) :B(obj1),
C(obj2) {}
        virtual ~D() {}

        virtual std::string get_obj_type() const
throw(std::exception,my_exception)
        {
                throw my_exception("which object?");
                return 0;
        }
};

int main(int argc, char ** argv)
{
        D d("stupid string", std::vector<int>());

        try
        {
                std::cout << "type: " << d.get_obj_type() << std::endl;
        }
        catch (my_exception const & error)
        {
                std::cerr << error.what() << std::endl;
                return -1;
        }

        return 0;
}

// CODE END

The compilation result is:

test_bis.cc:60: error: looser throw specifier for ‘virtual std::string
D::get_obj_type() const throw (std::exception, my_exception)’
test_bis.cc:40: error:   overriding ‘virtual std::string B::get_obj_type()
const throw (std::exception)’

I have no error when D only inherits from either B or C.


-- 
           Summary: wrong parse of exception declaration list in virtual
                    functions with multiple inheritence
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: florent_teichteil at yahoo dot fr
 GCC build triplet: amd64 (x86_64)
  GCC host triplet: amd64 (x86_64)
GCC target triplet: amd64 (x86_64)


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

Reply via email to