In following code std::move in lambda capture list felt unnecessary to me, but compiler does seem to need it.
As there is extra code for copying shared_ptr is generated if I don't use std::move.
Question is, why compiler can't optimise this on its own.
template<typename T>
std::function<void(void)> prepLambdaImpl(std::shared_ptr<T> aptr) {
#ifdef CONVERT_SHARED_PTR_TO_XVALUE
return [aptr=std::move(aptr)]
#else
return [aptr]
#endif
{
printf("use count: %ld\n", aptr.use_count());
};
}
Working example: https://godbolt.org/z/W3oWEjsjK