How are lambda identifiers registered?

I posted this answer that contains the code:

template <typename T>
auto vertex_triangle(const size_t index, const vector<pair<T, T>>& polygon) {
    const auto& first = index == 0U ? polygon.back() : polygon[index - 1U];
    const auto& second = polygon[index];
    const auto& third = index == size(polygon) - 1U ? polygon.front() : polygon[index + 1U];

    return [&](auto& output){ output.push_back(first);
                              output.push_back(second);
                              output.push_back(third); };
}

I thought that first, secondand thirdcan really be used as lambda identifiers, such as:

[first = index == 0U ? polygon.back() : polygon[index - 1U],
 second = polygon[index],
 third = index == size(polygon) - 1U ? polygon.front() : polygon[index + 1U]](auto& output){ output.push_back(first);
                                                                                             output.push_back(second);
                                                                                             output.push_back(third); };

But I only want to fix the permalink. Without specifying the type in the identifier, how can I do this?

+4
source share
1 answer

You cannot 1 . You cannot put the cv qualifier on the lambda capture list. You can take a look at the corresponding grammar :

init-capture:
    identifier initializer
    & identifier initializer

You also cannot specify the type in the capture list (see above).

, & :

[&first = index == 0U ? polygon.back() : polygon[index - 1U],
 &second = polygon[index],
 &third = index == size(polygon) - 1U ? polygon.front() : polygon[index + 1U]](auto& output){ output.push_back(first);
                                                                                             output.push_back(second);
                                                                                             output.push_back(third); };

, IMO.


1polygon - const, first, second third const, ! , , , .

+4

Source: https://habr.com/ru/post/1688811/


All Articles