나는 코드를 포함하는, this answer을 게시 한 :람다 식별자는 어떻게 캡쳐됩니까?
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); };
}
내가 생각하는 first
, second
및 third
정말이 같은 람다 식별자로 사용할 수 있습니다 :
[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); };
하지만 난 단지 원하는 일정한 참조로 캡처하기. 식별자에 유형을 지정하지 않으면 어떻게 할 수 있습니까?
람다의 각 인스턴스는 고유 한 이름이없는 클래스의 인스턴스입니다. 당신은 람다 (lambda) 나 그런 것에 식별자 (identifier)를 할당 할 수 없습니다. –
@ Sam. 나는 동의하지 않는다. 람다는 최고급 유형이다. 그리고 이것은 원래의 질문과 아무 관련이 없습니다. –
그래서 const에 대한 값으로 캡처하는 것을 피하기 위해 const 참조를 캡처하는 방법은 무엇입니까? BTW, 결국 코드 조각은 끔찍하게 읽을 수 없습니다. 이럴 경우 가치가 있다고 생각하지 않습니다. – StoryTeller