How to use lambda expressions outside of a function

Hi,

I am using lambda function inside of a funciton like the following code snippet and it compiles correctly.

void TRibbon::CheckMissingThirdDigit(const Uint& n) {
TDigit& Digit = *(next(Osdk.Digits.begin(), n ));
auto it = find_if(RvLines.begin(), RvLines.end(), [&] (auto & el) {
auto &Line { *el };
auto itc = find_if(Line.Cells.begin(), Line.Cells.end(), [&] (auto& ei) {
return ei->Digit.No == Digit.No;
} );
return itc == Line.Cells.end();
});
}
But when i try to define the lambda expression outside of the function
like the following I cant compile. I am getting an error 'Digit' was not declared in this scope


auto SearchvecLine = [&] (auto & el) {
auto &Line { *el };
auto itc = find_if(Line.Cells.begin(), Line.Cells.end(), [&] (auto& ei) {
return ei->Digit.No == Digit.No;
} );
return itc == Line.Cells.end();
};

void TRibbon::CheckMissingThirdDigit(const Uint& n) {
TDigit& Digit = *(next(Osdk.Digits.begin(), n ));
auto it = find_if(RvLines.begin(), RvLines.end(), SearchvecLine);


Thanks
'Digit' is a local reference in TRibbon::CheckMissingThirdDigit(). Obviously you can't use local names from global scope.
If you want to declare it outside of the function then you can no longer use a capture to get ahold of Digit. You need to pass it as a parameter. But now you're writing the lambda at the global scope, you're giving it a name, you can't use capture, should it still be a lambda?
Topic archived. No new replies allowed.