Reputation: 2127
I'm having problems trying to create the following lambda function:
const auto var x = [&y]() -> /*???*/ {
if (y == type1) {
return some_type_1;
} else if (y == type2) {
return some_type_2;
} else // ...
I know I cannot use auto as return type. But how can I do it in another way?
Thanks!
Upvotes: 1
Views: 106
Reputation: 157414
If some_type_1
and some_type_2
have a common type, write:
const auto var x = [&y]() -> typename std::common_type<
decltype(some_type_1),
decltype(some_type_2)>::type {
if (y == type1) {
return some_type_1;
} else if (y == type2) {
return some_type_2;
} else // ...
Equivalently, you can use a ternary expression:
const auto var x = [&y]() {
(y == type1) ? some_type_1 :
(y == type2) ? some_type_2 :
...;
}
Upvotes: 2