I have some code where I need to create unique-ids in some function to track some objects manipulated by that function. I thought about using the address of a callback which the function expects. The callbacks passed to the function are anonymous lambdas.
I call this function in many different places and, in every place, each lambda maintains its address across calls. That is, even though the lambdas are temporary, each individual lambda has its same address every time it's passed to the function.
This is nice, for my use case. But, why do they have the same address, and not a new one every time? Most importantly, is this reliable across compilers?
Compiler: MSVC 2019
// A method within some class.
void Method() {
// Helper lambda
auto makeSignal = [this](State& state, Data& data, int foo, auto&& onClick) {
DoMakeSignal(state, data, true, foo, 3.14, std::forward<decltype(onClick)>(onClick));
};
// The unique-id will be the address of the lambda.
// Everytime this 'Method' is called, this lambda
// will have the same address. Nice, but why?
makeSignal(GetState(), GetData(), 5, [this] {
log("signal callback");
});
makeSignal(GetState(), GetData(), 42, [this] {
log("signal callback");
});
}
// In another file
void DoMakeSignal(State& state, Data&, bool, int, float, auto&& onClick) {
uintptr_t uid = reinterpret_cast<uintptr_t>(&onClick);
state.mLastObjectUid = uid;
// ...
}