struct test
{
void func()
{
std::cout << (void*)this << std::endl;
}
};
int main()
{
auto f (&test::func);
auto g ((void(*)(test&))f); //line A
test t;
(t.*f)();
g(t); //line B
}
I'm concerned about whether this is guaranteed to work all the time or if this may not work on some implementations. For the two I tried it printed the same address twice, but I do not know if there were any problems with it.
Is the this pointer passed as the first parameter or last parameter like this? Would my cast be const test& for a const member function?
I believe this is misguided thinking - the fact that you get the addrees of T is
accidental/co-incidence.
The function g is expecting a reference to a test object - as we are all know that technicallyin theory references take up no space/memory - but
again we all know that passing references to function involves secretly passing
a pointer to the object.
So all in all - this is NOT gauaranteed to work - it all depends on how the compiler
actually implement references (the C++ standard says that this is implementation dependent).
So you were not getting a true this pointer - just an accidental one.
Last question: is it guaranteed to work if I passed a pointer to a test by value? (I did not originally mean to use a reference; it was, as was said, accidental luck)