I have the following code:
#include <stdio.h>
#include <functional>
template <typename T>
auto callback(T&& func) ->decltype(func())
{
return func();
}
double test(double& value)
{
value=value+1.0;
return value;
}
int main(void)
{
double t=1.0;
printf("%f\n",t);
test(t);
printf("%f\n",t);
callback(std::bind(test,t));
printf("%f\n",t);
}
And it outputs
1.000000
2.000000
2.000000
Which implies the callback
function got a copy of t
instead of a reference to t
. I am wondering what happened, since for std::bind
it should be perfect-forwarding.