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.
::
, and the pointer-to operator&
. As in&TestMessagePoller::poll_timer
. – Gillisusing
and make the memberpublic
in the first place. – Minierstd::cout << (void*)std::remove_reference_t<decltype(*m_sut)>::poll_timer
– Wolfsonstd::shared_ptr
andusing
that didn't even exist before C++11? The code you've shown requires at least C++11. – Wolfsonm_sut->poll_timer
is not a static function since C++11, so gcc is actually correct. Using -> on static members equals to usage on non-static, the rest is implementation-defined. Ifm_sut
is anullptr
, you get an UB. Also, returned value tby clang isn't equivalent of pointer to member function, it's some odd value. – Pardue