Suppose you have a function that takes a std::vector
of any type and processes it in some way:
template<typename T>
void foo(std::vector<T> &vec) {
// work with vec
}
Since C++14
, we are able to achieve the same thing with lambdas. In this case, we call them generic lambdas, since we introduce a template-like deduction to them:
auto foo_lambda = [](std::vector<auto> &vec) {
// work with vec
};
But our options seem quite limited to me. Suppose that I not only have to introduce a type deduction, but I also need to introduce template values. For example, let's change std::vector
to std::array
:
template<typename T, std::size_t size>
void foo(std::array<T, size> &arr) {
// work with arr
}
When dealing with template functions, we are able to introduce a template value, which can be used to match argument's needs. Neat.
I wanted to achieve the same functionality with generic lambdas, but I was unable to do so.
Is there a way to introduce a similar, deduced value to a lambda expression so any std::array
s can be used with said lambda, similarily to the second version of the foo()
function above?
EDIT: As stated in the comments by Evg, my vector<auto>
syntax is non-standard GCC extension. For details see this answer referring to this document.