How can I explicitly specify the type of a captured variable for a lambda in C++?
For example, assume I have a function that takes a universal reference and I want to perfect-forward it into a lambda.
I found out, that I can use a std::tuple
for this as shown below, but I wonder if there is a more succinct way.
template<typename T>
auto returns_functor (T&& value)
{
return [value = std::tuple<T> (std::forward<T> (value))] ()
{
/* use std::get<0> (value) */
};
}
Related: Capturing perfectly-forwarded variable in lambda (the accepted answer there suggests this is a different question, but further answers give essentially the above solution).