34

Lets say I have these lines of code;

std::vector<int> ints;
std::for_each(ints.begin(), ints.end(), [](int& val){ val = 7; });

However, I dont want to specify the argument type in my lambda functions, ie, I want to write something like this;

std::for_each(ints.begin(), ints.end(), [](auto& val){ val = 7; });

Is there anyway this can be achieved?

(boost::lambda doesn't need types to be specified...)


Update:

For now I use a macro: #define _A(container) decltype(*std::begin(container)) so I can do:

std::for_each(ints.begin(), ints.end(), [](_A(ints)& val){ val = 7; });
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90

4 Answers4

30

No. "Polymorphic lambdas" is what this feature was referred to during the C++ committee discussions, and it was not standardized. The parameter types of a lambda must be specified.

You can use decltype though:

std::for_each(ints.begin(), ints.end(), [](decltype(*ints.begin())& val){ val = 7; });
Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
15

Your preferred syntax is legal as of C++14, and is referred to as a generic lambda or polymorphic lambda.

http://isocpp.org/blog/2013/04/n3649-generic-polymorphic-lambda-expressions-r3

auto lambda = [](auto x) { return x; };
lambda(5);
lambda("hello");
lambda(std::vector<int>({5, 4, 3}));

As of C++20, we can also use this syntax for regular functions:

auto f(auto x) { return x; }
David Stone
  • 26,872
  • 14
  • 68
  • 84
3

If you have a container you may try something like this

template<typename Container>
void reset(Container c)
{
   for_each(c.begin(),c.end(),[](typename Container::reference val) { val=7; });
}
O.C.
  • 6,711
  • 1
  • 25
  • 26
  • If your function is not a template function but works on a concrete type (as is my case), you can simply do: `std::for_each(ints.begin(), ints.end(), [](ints::reference val) { ... });` – Guss Apr 17 '13 at 12:59
0

Try this:

#include <functional>
#include <algorithm>
#include <iostream>

template <typename ValTy>
std::function<void(ValTy&)> polymorphicLambda ()
{
    return std::function<void(ValTy&)> ([](ValTy& val) -> void { val = 7; } );
}

int main()
{
    std::vector<int> ints(5);

    std::generate_n(ints.begin(), 5, []() { return 0; });
    std::for_each(ints.begin(), ints.end(), [](int& val) { std::cout << val << "\t"; });
    std::cout << std::endl;

    std::for_each(ints.begin(), ints.end(), polymorphicLambda<int>());
    std::for_each(ints.begin(), ints.end(), [](int& val) { std::cout << val << "\t"; });
    std::cout << std::endl;


    std::vector<double> doubles(5);

    std::generate_n(doubles.begin(), 5, []() { return 0; });
    std::for_each(doubles.begin(), doubles.end(), [](double& val) { std::cout << val << "\t"; });
    std::cout << std::endl;

    std::for_each(doubles.begin(), doubles.end(), polymorphicLambda<double>());
    std::for_each(doubles.begin(), doubles.end(), [](double& val) { std::cout.precision(2); std::cout << std::fixed << val << "\t"; });
    std::cout << std::endl;

    return 0;
}

You might also be able to do some funky stuff with lambdas that don't return void and also with variadic templates to pass in multiple params to the lambda.