To be on the safe side I am already using old style function pointers in my DLL calls like that:
// DLL
typedef int (__stdcall* ty)();
void test(ty t)
{
if (t)
{
int r = t();
....
}
}
Whereas I could use this one:
void test(std::function<int()> t)
{
}
The latter however, as known, uses type erasure. std::function
cannot be a raw function pointer (for it may be passed a lambda which has captures and thus, cannot be a raw pointer).
So, in Visual studio, using a DLL build in release mode with a function signature that contains std::function
crashes, when used from an executable build in debug mode and vice versa. Even if it's both debug or release mode, the behavior is not the same. Sometimes it crashes, some times it works.
Is there a defined run-time behaviour that we can rely on to use std::function
? Or this is a compile-only thing that is specialized by the compiler depending on what I pass to it and therefore, run-time behaviour cannot be assumed behorehand?
At the assembly level the function signature on a precompiled unit must be known. As far I as know std::function
runtime implementation is not well defined.
So, for example, when I'm compiling, then
void test(std::function<int()> t)
can take any argument like []() -> int, [&]() -> int, [=]() -> int
, etc via type erasure. But when this is precompiled and thus runtime only, what can be accepted? How std::function is implemented? With a class pointer? Is there a well defined way?
I'm not looking for a necessarily VS-related solution, but a standard definition of std::function
, if any.