Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC bug when using -Woverloaded-virtual flag

Tags:

c++

gcc

boost

Let's take this code:

#include <boost/asio/impl/error.ipp>

struct Type : public boost::system::error_category
{
    const char * name() const noexcept override 
    {
        return nullptr;
    }

    std::string message(int) const override
    {
        return {};
    }
};

As we can see, it compiles without warnings.

Now let’s create identical code (the method prototypes are copied one-to-one from the boost ones):

#include <string>

struct Base
{
    virtual std::string message(int) const = 0;
    virtual char const * message(int, char *, std::size_t) const noexcept
    {
        return nullptr;
    }
};


struct Deriv : Base
{
    std::string message(int) const
    {
        return {};
    }
};

And suddenly, the compiler starts talking about hiding the method. What is this? GCC compiler bug? Because Clang, for example, does not give a warning in this case.

like image 947
d7d1cd Avatar asked May 15 '26 14:05

d7d1cd


1 Answers

Maybe this is a misinterpretation of the warning message?

If your class Deriv defines only one method from the overloaded ones in Base, all other overloads are hidden, even if they are virtual. For me the warning seems to be correct!

Simplified example:


struct Base 
{
    virtual void f1() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
    virtual void f1( int i) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};


struct Deriv : Base 
{
    // using Base::f1; // << add this line to get f1( int ) reachable
    void f1() override )
    {    
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }    
};

int main()
{
    Deriv d;
    d.f1();
    d.f1(1);
}

This simply did not only warn, it also raises an error as f1() can not be reached!

You simply can add all other overloads to your derived class by adding using Base::f1; into Deriv.

The given goodbolt link to fix the boost thing is here: godbolt It can simply be fixed with adding the using statement.

like image 103
Klaus Avatar answered May 19 '26 02:05

Klaus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!