c++gccclang++compiler-specific

GCC compiler produces error: invalid use of member function while CLang compiler does NOT


I'm using the following program:

In the main function, I want to print the address of the poll_timer function.

The program compiles and runs successfully with clang but not with GCC.

I get the following error with GCC

"709568706/source.cpp: In function ‘int main()’:
709568706/source.cpp:28:32: error: invalid use of member function ‘static void MessagePoller::poll_timer()’ (did you forget the ‘()’ ?)
     std::cout << (void*)m_sut->poll_timer << std::endl;
                         ~~~~~~~^~~~~~~~~~"
#include <iostream>
#include <memory>

class MessagePoller
{
  protected:
    static void poll_timer()
    {
        std::cout << "Poll timer Base called\n";
    }
};

class TestMessagePoller : public MessagePoller
{
public:
    using MessagePoller::poll_timer;

};
typedef std::shared_ptr<TestMessagePoller> TestMessagePollerPtr;

int main()
{   
    TestMessagePollerPtr m_sut;
    m_sut = TestMessagePollerPtr(new TestMessagePoller());

    std::cout << "HERE1\n";
    m_sut->poll_timer();
    std::cout << (void*)m_sut->poll_timer << std::endl;

    return 0;
    
}

I have tried one thing, removing the "using" statement and changing the access of the poll_timer to public and that worked. But I would like to know what is going on with the program as is.


Solution

  • Yes, this is a bug in gcc; I've filed https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109958.

    The bug appears to have first appeared in the 4.8 branch, so you might consider downgrading to 4.7.4 if that's an option.

    Workarounds are:


    The code is valid; & may be used on a class member access operation (. or ->) where the operand designates a static member function (but not an explicit object member function). Non-static member functions are explicitly excluded here.