So I have a lambda who's return type is auto
and I'm having issues with the array backing for an initializer_list
being destroyed here:
const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; };
I will be using the lambda like this:
auto bar = foo(1, 2, 3);
for(const auto& i : bar) cout << i << endl;
A job that I'm working has as part of their coding standard that all lambdas are single statements (feel free to express your outrage.) I think that I can get around this by:
- Giving
foo
a return type ofvector int
, but that messes up how nicely generic it is:const auto foo = [](const auto& a, const auto& b, const auto& c) -> vector<int> { return {a, b, c}; }
- Just writing a templatized function which constructs a
vector<T>
and returns it:template <typename T> vector<T> foo(const T& a, const T& b, const T& c){ return {a, b, c}; }
Is it possible to coerce these variables into a container, who's backing won't be destroyed all in one line so that I can keep the lambda with an auto
return type?