Pointer to function wrong conversion [duplicate] - c++

I am using luabind as my lua to C++ wrapper. Luabind offers a method to use my own callback function to handle exceptions thrown by lua, set_pcall_callback(). So I paraphrased an example from the documentation, the changes being the logger->log() function and putting the function in a class called 'Engine', so instead of it being a regular global function it is now a member function, which is where my problem seems to be.
Here are the relevant code snips:
class Engine //Whole class not shown for brevity
{
public:
Engine();
~Engine();
void Run();
int pcall_log(lua_State*);
private:
ILogger *logger;
};
Engine::Run()
{
lua_State* L = lua_open();
luaL_openlibs(L);
open(L);
luabind::set_pcall_callback(&Engine::pcall_log); //<--- Problem line
//etc...rest of the code not shown for brevity
}
int Engine::pcall_log(lua_State *L)
{
lua_Debug d;
lua_getstack( L,1,&d);
lua_getinfo( L, "Sln", &d);
lua_pop(L, 1);
stringstream ss;
ss.clear();
ss.str("");
ss << d.short_src;
ss << ": ";
ss << d.currentline;
ss << ": ";
if ( d.name != 0)
{
ss << d.namewhat;
ss << " ";
ss << d.name;
ss << ") ";
}
ss << lua_tostring(L, -1);
logger->log(ss.str().c_str(),ELL_ERROR);
return 1;
}
Here is what the compiler says during compilation:
C:\pb\engine.cpp|31|error: cannot convert 'int (Engine::*)(lua_State*)' to 'int (*)(lua_State*)' for argument '1' to 'void luabind::set_pcall_callback(int (*)(lua_State*))'|
So it seems that the error is that the function expects a regular function pointer, not a class member function pointer. Is there a way to cast or use an intermediate function pointer to pass to the set_pcall_callback() function?
Thank you!

No. A member function is not a free function. The type is entirely different, and a pointer to a member function (PTMF) is a completely different, incompatible object from a function pointer. (A PTMF is usually much bigger, for example.) Most importantly a pointer-to-member must always be used together with an instance pointer to the object whose member you want to call, so you cannot even use a PTMF the same way you use a function pointer.
The easiest solution for interacting with C code is to write a global wrapper function that dispatches your call, or to make your member function static (in which case it becomes essentially a free function):
// global!
Engine * myEngine;
int theCallback(lua_State * L)
{
return myEngine->pcall_log(L);
}
Engine::Run()
{
/* ... */
myEngine = this;
luabind::set_pcall_callback(&theCallback);
/* ... */
}
The conceptual problem here is that you have an engine class, although you will practically only have one single instance of it. For a genuine class with many objects, a PTMF wouldn't make sense because you'd have to specify which object to use for the call, whereas your engine class perhaps is essentially a singleton class which could be entirely static (i.e. a glorified namespace).

Not suitable for your LUA problem, but maybe on other libraries:
If a function requests a a function pointer like func(void* param, ...) and you can ensure that the lifetime of your object is greater than the stored function pointer, then you could technically also use a method pointer (looks the same on the stack), but C++ prevents direct casting of method pointers to function pointers.
But with a little trick, you can also cast method pointers to function pointers:
template<typename M> inline void* GetMethodPointer(M ptr)
{
return *reinterpret_cast<void**>(&ptr);
}
Using that, you can use method pointers for example with libmicrohttpd:
this->m_pDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, this->m_wPort, NULL, NULL, reinterpret_cast<MHD_AccessHandlerCallback>(GetMethodPointer(&CMyWebServer::AccessHandlerCallback)), this, MHD_OPTION_END);
But be aware of it. You must take care of the lifetime of that object. Also the calling conventions must match.

As a callback, it is usual to use static functions:
class Engine //Whole class not shown for brevity
{
....
static int pcall_log(lua_State*);
...
}
This would solve your issue.

Explicit conversion from method pointer to function pointer is illegal in C++ - period.
But there is a hack. We have to first convert the (const) method pointer to (const) void* and then to (const) function pointer. And it works. And why wouldn't it? Everything and I mean everything can be pointed to by a void* because everything has an address.
WARNING: The below is DAAEINGEROUS hack territory. If you're developing software for a fighter jet or whatnot, you should know better than to use this. I'm not responsible! I'm just providing this here for educational purposes.
The trick is we must convert between method pointer and function pointer through an intermediate conversion to, potentially cv (const-volatile) qualified, void*.
This way we are able to call a member function (pointer) through a function pointer, with the first argument being a Type* pointer to the target class object, which is equivalent to the member function call's this*.
Given a:
MethodPointerType f;
then
FunctionPointerType m_pFn = reinterpret_cast<FunctionPointerType>( reinterpret_cast<void*&>( f ) );
or to make it more explicit use two of the following in sequence, for non-const and const member functions:
template<typename MP>
void* getMethodVoidPointer( MP ptr )
{
return *reinterpret_cast<void**>( &ptr );
}
template<typename FP>
FP getFunctionPointer( void* p )
{
return reinterpret_cast<FP>( p );
}
template<typename MP>
const void* getMethodConstVoidPointer( MP ptr )
{
return *reinterpret_cast<const void**>( &ptr );
}
template<typename FP>
FP getConstFunctionPointer( const void* p )
{
return reinterpret_cast<FP>( p );
}
Play with it live here with a complete compilable sample in C++17: https://onlinegdb.com/HybR8crqw
It works in Visual Studio too.

Related

Pass a C++ member function as a callback function [duplicate]

I am using luabind as my lua to C++ wrapper. Luabind offers a method to use my own callback function to handle exceptions thrown by lua, set_pcall_callback(). So I paraphrased an example from the documentation, the changes being the logger->log() function and putting the function in a class called 'Engine', so instead of it being a regular global function it is now a member function, which is where my problem seems to be.
Here are the relevant code snips:
class Engine //Whole class not shown for brevity
{
public:
Engine();
~Engine();
void Run();
int pcall_log(lua_State*);
private:
ILogger *logger;
};
Engine::Run()
{
lua_State* L = lua_open();
luaL_openlibs(L);
open(L);
luabind::set_pcall_callback(&Engine::pcall_log); //<--- Problem line
//etc...rest of the code not shown for brevity
}
int Engine::pcall_log(lua_State *L)
{
lua_Debug d;
lua_getstack( L,1,&d);
lua_getinfo( L, "Sln", &d);
lua_pop(L, 1);
stringstream ss;
ss.clear();
ss.str("");
ss << d.short_src;
ss << ": ";
ss << d.currentline;
ss << ": ";
if ( d.name != 0)
{
ss << d.namewhat;
ss << " ";
ss << d.name;
ss << ") ";
}
ss << lua_tostring(L, -1);
logger->log(ss.str().c_str(),ELL_ERROR);
return 1;
}
Here is what the compiler says during compilation:
C:\pb\engine.cpp|31|error: cannot convert 'int (Engine::*)(lua_State*)' to 'int (*)(lua_State*)' for argument '1' to 'void luabind::set_pcall_callback(int (*)(lua_State*))'|
So it seems that the error is that the function expects a regular function pointer, not a class member function pointer. Is there a way to cast or use an intermediate function pointer to pass to the set_pcall_callback() function?
Thank you!
No. A member function is not a free function. The type is entirely different, and a pointer to a member function (PTMF) is a completely different, incompatible object from a function pointer. (A PTMF is usually much bigger, for example.) Most importantly a pointer-to-member must always be used together with an instance pointer to the object whose member you want to call, so you cannot even use a PTMF the same way you use a function pointer.
The easiest solution for interacting with C code is to write a global wrapper function that dispatches your call, or to make your member function static (in which case it becomes essentially a free function):
// global!
Engine * myEngine;
int theCallback(lua_State * L)
{
return myEngine->pcall_log(L);
}
Engine::Run()
{
/* ... */
myEngine = this;
luabind::set_pcall_callback(&theCallback);
/* ... */
}
The conceptual problem here is that you have an engine class, although you will practically only have one single instance of it. For a genuine class with many objects, a PTMF wouldn't make sense because you'd have to specify which object to use for the call, whereas your engine class perhaps is essentially a singleton class which could be entirely static (i.e. a glorified namespace).
Not suitable for your LUA problem, but maybe on other libraries:
If a function requests a a function pointer like func(void* param, ...) and you can ensure that the lifetime of your object is greater than the stored function pointer, then you could technically also use a method pointer (looks the same on the stack), but C++ prevents direct casting of method pointers to function pointers.
But with a little trick, you can also cast method pointers to function pointers:
template<typename M> inline void* GetMethodPointer(M ptr)
{
return *reinterpret_cast<void**>(&ptr);
}
Using that, you can use method pointers for example with libmicrohttpd:
this->m_pDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, this->m_wPort, NULL, NULL, reinterpret_cast<MHD_AccessHandlerCallback>(GetMethodPointer(&CMyWebServer::AccessHandlerCallback)), this, MHD_OPTION_END);
But be aware of it. You must take care of the lifetime of that object. Also the calling conventions must match.
As a callback, it is usual to use static functions:
class Engine //Whole class not shown for brevity
{
....
static int pcall_log(lua_State*);
...
}
This would solve your issue.
Explicit conversion from method pointer to function pointer is illegal in C++ - period.
But there is a hack. We have to first convert the (const) method pointer to (const) void* and then to (const) function pointer. And it works. And why wouldn't it? Everything and I mean everything can be pointed to by a void* because everything has an address.
WARNING: The below is DAAEINGEROUS hack territory. If you're developing software for a fighter jet or whatnot, you should know better than to use this. I'm not responsible! I'm just providing this here for educational purposes.
The trick is we must convert between method pointer and function pointer through an intermediate conversion to, potentially cv (const-volatile) qualified, void*.
This way we are able to call a member function (pointer) through a function pointer, with the first argument being a Type* pointer to the target class object, which is equivalent to the member function call's this*.
Given a:
MethodPointerType f;
then
FunctionPointerType m_pFn = reinterpret_cast<FunctionPointerType>( reinterpret_cast<void*&>( f ) );
or to make it more explicit use two of the following in sequence, for non-const and const member functions:
template<typename MP>
void* getMethodVoidPointer( MP ptr )
{
return *reinterpret_cast<void**>( &ptr );
}
template<typename FP>
FP getFunctionPointer( void* p )
{
return reinterpret_cast<FP>( p );
}
template<typename MP>
const void* getMethodConstVoidPointer( MP ptr )
{
return *reinterpret_cast<const void**>( &ptr );
}
template<typename FP>
FP getConstFunctionPointer( const void* p )
{
return reinterpret_cast<FP>( p );
}
Play with it live here with a complete compilable sample in C++17: https://onlinegdb.com/HybR8crqw
It works in Visual Studio too.

How to assign a function to a function pointer that takes argument of type void*?

I'm using a library provided by my professor for a class assignment so I can't reveal too much of my code. Here's part of the API:
typedef void (*thread_startfunc_t) (void*);
int thread_libinit(thread_startfunc_t func, void *arg);
int thread_create(thread_startfunc_t func, void *arg);
thread_libinit initializes the thread library. A user program should call
thread_libinit exactly once (before calling any other thread functions).
thread_libinit creates and runs the first thread. This first thread is
initialized to call the function pointed to by func with the single
argument arg. Note that a successful call to thread_libinit will not
return to the calling function. Instead, control transfers to func, and
the function that calls thread_libinit will never execute again.
thread_create is used to create a new thread. When the newly created
thread starts, it will call the function pointed to by func and pass it the
single argument arg.
To test I wrote the following code:
void pHello(int i)
{
cout << "Hello from thread " << i << endl;
}
... [In Main] ...
typedef void (*thread_startfunc_t) (void* i);
thread_startfunc_t pSalute = & pHello; //Does not compile
thread_libinit(pSalute,1);
I receive the error:
In function `int main(int, char**)`:
error: invalid conversion from `void (*) (int)' to `thread_startfunc_t{aka void(*) (void*)}' [-fpermissive]
I thought a void pointer variable can point to any variable. So why can't it point to the int of the function pHello? How do I assign a function to the function pointer?
A function pointer with a specific signature (given as return type plus variable types) can only point to functions having the same signature.
In your case, where it seems you can't change the function pointer type, you can change your function pHello according to
void pHello(void* i)
{
std::cout << "Hello from thread " << *static_cast<int*>(i) << std::endl;
}
typedef void (*thread_startfunc_t) (void*);
int main()
{
thread_startfunc_t f = pHello;
//or point to a non-capturing lambda:
thread_startfunc_t f2 = [](void *) { std::cout<<"hi"<<std::endl; };
}
This is however not really nice C++ code.
I thought a void pointer variable can point to any variable. So why
can't it point to the int of the function pHello?
Because the statement is not correct. In C++, you must use casts for this. This is one of the big differences between C++ and C. See also Difference between void pointers in C and C++
You can test this. Compile your code as C, remove C++ features like <iostream>, and it should work as intended.
How do I assign a
function to the function pointer?
Change the signature of pHello to:
void pHello(void*)
Inside the function, you must use casts to obtain the value of the pointee.
Also, when you call thread_libinit, the second argument must be the address of an int object which lives until the function returns.
Of course, none of this is very nice or modern C++. Your professor's API should instead make use of std::function.

How to write a function pointer to a function returning a function pointer to a function?

I want to assign a function's address to a function pointer, but the function to be addressed returns a function pointer with the same signature as itself, causing it to recurse in a way that I can't write the return type at all, for the function pointer or even the function declaration itself...
I guess a way of simplifying the problem so it's not confusing:
How could I write a function declaration such, that it can return a pointer to itself (or any other function with the same signature)?
????? function(int a){
// could be this function, or another with the same signature,
return arbitraryFunction;
}
?????(*func)(int) = function; // same problem as above
edit:
For now I have a solution, though I won't post it as an answer because it's aggressively ugly. It gets rid of the recursion by simply returning a raw void* pointer as the return type, and ends up taking the following form:
void* function(int parameter){
return arbitraryFunction; // of the same signature
}
void*(*func)(int) = function;
func = reinterpret_cast<void*(*)(int)>(func(42)); // sin
edit2:
It seems casting between function pointers and regular pointers is UB, so I can't use void* in this case...
To answer one of the comments, this is for passing control between multiple "main" loops in my program, with each loop getting it's own function. There's a lot of ways to do this, but returning function pointers (or NULL to terminate the program) mid-loop seemed like the simplest method, but I didn't anticipate that pointers to data and pointers to function addresses would be incompatable with each other. I think returning polymorphic function objects will end up being the more sane option in this case.
Don't use void*, because no guarantee that a void * can hold a function pointer. You can use void(*)() as a workaround:
typedef void(*void_func)();
typedef void_func (*func_type) (int);
void_func arbitraryFunction(int a) {
// could be this function, or another with the same signature,
cout << "arbitraryFunction\n";
return nullptr;
}
void_func function(int a) {
// could be this function, or another with the same signature,
return (void_func) arbitraryFunction;
}
int main() {
// your code goes here
func_type f = (func_type) function(0);
f(0);
return 0;
}
LIVE
C99 [6.2.5/27]:
A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Similarly, pointers to
qualified or unqualified versions of compatible types shall have the
same representation and alignment requirements. All pointers to
structure types shall have the same representation and alignment
requirements as each other. All pointers to union types shall have the
same representation and alignment requirements as each other. Pointers
to other types need not have the same representation or alignment
requirements.
C99 [6.3.2.3/8]:
A pointer to a function of one type may be converted to a pointer to a function of another type and back again; the result shall compare
equal to the original pointer.
The trick in C is to take advantage of the fact that any kind of function pointer can be cast to any other kind of function pointer:
#include <stdlib.h>
#include <stdio.h>
typedef void(*emptyfunc)(void);
typedef emptyfunc (*funcptr2)(int);
funcptr2 strategy(int m)
{
printf("Strategy %d.\n", m);
return (funcptr2)&strategy;
}
int main (void)
{
const funcptr2 strategy2 = (funcptr2)strategy(1);
const funcptr2 strategy3 = (funcptr2)strategy2(2);
strategy3(3);
return EXIT_SUCCESS;
}
Each pointer to a strategy is always in a type that can be called once, and the return value is put back into a form that can be called once again.
In C++, you would declare a function object:
class strategy {
public:
virtual const strategy& operator()(int) const = 0;
}
An instance of this class can be called like a function.
I suspect what you are trying to do is a more complex version of something like this:
typedef MainLoop *MainLoop(); // not legal
extern MainLoop* main_loop_1();
extern MainLoop* main_loop_2();
MainLoop* main_loop_1()
{
// do some work here
return main_loop_2;
}
MainLoop* main_loop_2()
{
// do some work here
return main_loop_1;
}
int main()
{
MainLoop f = main_loop_1;
for (;;) {
f = f();
}
}
A workaround is to wrap the function pointer in a struct:
struct MainLoop {
MainLoop (*function_ptr)(); // legal
};
extern MainLoop main_loop_1();
extern MainLoop main_loop_2();
MainLoop main_loop_1()
{
// do some work here
return {main_loop_2};
}
MainLoop main_loop_2()
{
// do some work here
return {main_loop_1};
}
int main()
{
MainLoop f{main_loop_1};
for (;;) {
f = f.function_ptr();
}
}

C++ class member function pointer to function pointer

I am using luabind as my lua to C++ wrapper. Luabind offers a method to use my own callback function to handle exceptions thrown by lua, set_pcall_callback(). So I paraphrased an example from the documentation, the changes being the logger->log() function and putting the function in a class called 'Engine', so instead of it being a regular global function it is now a member function, which is where my problem seems to be.
Here are the relevant code snips:
class Engine //Whole class not shown for brevity
{
public:
Engine();
~Engine();
void Run();
int pcall_log(lua_State*);
private:
ILogger *logger;
};
Engine::Run()
{
lua_State* L = lua_open();
luaL_openlibs(L);
open(L);
luabind::set_pcall_callback(&Engine::pcall_log); //<--- Problem line
//etc...rest of the code not shown for brevity
}
int Engine::pcall_log(lua_State *L)
{
lua_Debug d;
lua_getstack( L,1,&d);
lua_getinfo( L, "Sln", &d);
lua_pop(L, 1);
stringstream ss;
ss.clear();
ss.str("");
ss << d.short_src;
ss << ": ";
ss << d.currentline;
ss << ": ";
if ( d.name != 0)
{
ss << d.namewhat;
ss << " ";
ss << d.name;
ss << ") ";
}
ss << lua_tostring(L, -1);
logger->log(ss.str().c_str(),ELL_ERROR);
return 1;
}
Here is what the compiler says during compilation:
C:\pb\engine.cpp|31|error: cannot convert 'int (Engine::*)(lua_State*)' to 'int (*)(lua_State*)' for argument '1' to 'void luabind::set_pcall_callback(int (*)(lua_State*))'|
So it seems that the error is that the function expects a regular function pointer, not a class member function pointer. Is there a way to cast or use an intermediate function pointer to pass to the set_pcall_callback() function?
Thank you!
No. A member function is not a free function. The type is entirely different, and a pointer to a member function (PTMF) is a completely different, incompatible object from a function pointer. (A PTMF is usually much bigger, for example.) Most importantly a pointer-to-member must always be used together with an instance pointer to the object whose member you want to call, so you cannot even use a PTMF the same way you use a function pointer.
The easiest solution for interacting with C code is to write a global wrapper function that dispatches your call, or to make your member function static (in which case it becomes essentially a free function):
// global!
Engine * myEngine;
int theCallback(lua_State * L)
{
return myEngine->pcall_log(L);
}
Engine::Run()
{
/* ... */
myEngine = this;
luabind::set_pcall_callback(&theCallback);
/* ... */
}
The conceptual problem here is that you have an engine class, although you will practically only have one single instance of it. For a genuine class with many objects, a PTMF wouldn't make sense because you'd have to specify which object to use for the call, whereas your engine class perhaps is essentially a singleton class which could be entirely static (i.e. a glorified namespace).
Not suitable for your LUA problem, but maybe on other libraries:
If a function requests a a function pointer like func(void* param, ...) and you can ensure that the lifetime of your object is greater than the stored function pointer, then you could technically also use a method pointer (looks the same on the stack), but C++ prevents direct casting of method pointers to function pointers.
But with a little trick, you can also cast method pointers to function pointers:
template<typename M> inline void* GetMethodPointer(M ptr)
{
return *reinterpret_cast<void**>(&ptr);
}
Using that, you can use method pointers for example with libmicrohttpd:
this->m_pDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, this->m_wPort, NULL, NULL, reinterpret_cast<MHD_AccessHandlerCallback>(GetMethodPointer(&CMyWebServer::AccessHandlerCallback)), this, MHD_OPTION_END);
But be aware of it. You must take care of the lifetime of that object. Also the calling conventions must match.
As a callback, it is usual to use static functions:
class Engine //Whole class not shown for brevity
{
....
static int pcall_log(lua_State*);
...
}
This would solve your issue.
Explicit conversion from method pointer to function pointer is illegal in C++ - period.
But there is a hack. We have to first convert the (const) method pointer to (const) void* and then to (const) function pointer. And it works. And why wouldn't it? Everything and I mean everything can be pointed to by a void* because everything has an address.
WARNING: The below is DAAEINGEROUS hack territory. If you're developing software for a fighter jet or whatnot, you should know better than to use this. I'm not responsible! I'm just providing this here for educational purposes.
The trick is we must convert between method pointer and function pointer through an intermediate conversion to, potentially cv (const-volatile) qualified, void*.
This way we are able to call a member function (pointer) through a function pointer, with the first argument being a Type* pointer to the target class object, which is equivalent to the member function call's this*.
Given a:
MethodPointerType f;
then
FunctionPointerType m_pFn = reinterpret_cast<FunctionPointerType>( reinterpret_cast<void*&>( f ) );
or to make it more explicit use two of the following in sequence, for non-const and const member functions:
template<typename MP>
void* getMethodVoidPointer( MP ptr )
{
return *reinterpret_cast<void**>( &ptr );
}
template<typename FP>
FP getFunctionPointer( void* p )
{
return reinterpret_cast<FP>( p );
}
template<typename MP>
const void* getMethodConstVoidPointer( MP ptr )
{
return *reinterpret_cast<const void**>( &ptr );
}
template<typename FP>
FP getConstFunctionPointer( const void* p )
{
return reinterpret_cast<FP>( p );
}
Play with it live here with a complete compilable sample in C++17: https://onlinegdb.com/HybR8crqw
It works in Visual Studio too.

Could someone please explain the difference between a "reference" and a "pointer" in this case?

When I read litb answer to this question, I learned that passing an array by reference allows us to obtain its size. I just played little bit with code, and tried to pass a "function" by reference and surprisingly (at least for me), this code compiles:
void execute( void (&func)() ) // func is passed by reference!
{
func();
}
Is there any difference between the last function, and this one:
void execute( void (*func)() ) // func is passed by pointer!
{
func();
}
I tried it using VC2008, and it produces different output in each case. The strange thing is that the compiler optimizes the code better in case of a function pointer:
void print()
{
std::cout << "Hello References!";
}
void execute( void (&func)() ) // optimized
{
func();
}
int main()
{
00291020 call print (291000h)
}
=========================================
// In this case, the compiler removes all function calls in the code!
void print() // optimized!
{
std::cout << "Hello Pointers!";
}
void execute( void (*func)() ) // optimized
{
func();
}
int main()
{
002F1005 push offset string "Hello References!" (2F2124h)
002F100A push eax
002F100B call std::operator<<<std::char_traits<char> > (2F1150h)
}
There has to be a difference, although I don't see it, right?
Note: the code was compiled using VC2008, with /O2 and /Ot turned on.
EDIT:: I am really interested about any difference between function references and function pointers. I examined the produced assembly code just to see how it is translated in each case.
For the language difference (keeping only the function declarations below, since that's what's important only)
void execute( void (&func)() );
void g();
int main() {
void (*fp)() = g;
execute(fp); // doesn't work
execute(&g); // doesn't work either
execute(g); // works
}
It doesn't work, because it wants a function, not a function pointer. For the same reason that array answer rejects a pointer, this rejects a pointer too. You have to pass "g" directly.
For templates, it matters too
template<typename T>
void execute(T &t) { T u = t; u(); }
template<typename T>
void execute(T t) { T u = t; u(); }
Those two are very different from one another. If you call it with execute(g); like above, then the first will try to declare a function and initialize it with t (reference to g). The generated function would look like this
void execute(void(&t)()) { void u() = t; u(); }
Now you can initialize references and pointers to functions, but of course not functions itself. In the second definition, T will be deduced to a function pointer type by template argument deduction, and passing a function will convert it to that pointer parameter type implicitly. So everything will go fine.
I don't know why MSVC treats them differently for inlining - but i also suspect it's because function references appear more seldom.
It's not as common an idiom, so it might just be that the VS team didn't add a rule to optimise it.
I think it is due to the C++ Standard 4.3:
An lvalue of function type T can be converted to an rvalue of type “pointer to T.” The result is a pointer to
the function.
The difference between a reference(&) and pointer(*) is that the reference provides the address of the variable or the location, and the pointer points to the location in memory of the address stored in it.
int *pointer;
int variable;
pointer = &variable; // assigning the address of variable to pointer
variable = 53; // value of variable
cout << *pointer; // This should output the value of the address where is pointing, in this
// case 53, that is the value of variable to where is pointing.
We can conclude that the (&variable) have the address of that memory location and *anyname points to the address stored in its memory...