Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access auto parameter's type within lambda

I'm using a lambda function for boilerplate code:

auto import = [&](auto & value){
   // Do some stuff
};

As value is in fact a std::vector, I need to access its value_type static member to call a template function on one of its element.

I tried use of decltype without success :

auto import = [&](auto & value){
   decltype(value)::value_type v;
};

Is there any way to do so ?

like image 747
etham Avatar asked Oct 18 '25 16:10

etham


1 Answers

The type of value is an lvalue-reference, you can't get the member type from it and have to remove the reference part, e.g

typename std::decay_t<decltype(value)>::value_type v;

PS: You also need to add typename in advance (as @Vlad answered) for the dependent type name. See Where and why do I have to put the “template” and “typename” keywords?.

LIVE

like image 116
songyuanyao Avatar answered Oct 20 '25 07:10

songyuanyao