Search code examples
c++templatesc++14autodecltype

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 ?


Solution

  • 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