Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using alias for referenced variables in capture list of lambda?

With a lambda, is it not possible to use an alias for a variable that is in the capture list (by reference)?

auto AddSlip = [rFile = &fileCSV](COleDateTime datMeeting)
{

}

That won’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With a lambda, is it not possible to use an alias for a variable that is in the capture list (by reference)?

Yes: it’s possible. Starting from C++14.

Not in your way

auto AddSlip = [rFile = &fileCSV](COleDateTime datMeeting)

because rFile become the copy of the pointer to fileCSV; the syntax you’re looking for is the following

auto AddSlip = [&rFile = fileCSV](COleDateTime datMeeting)

or, if you want a const reference,

auto AddSlip = [&rFile = std::as_const(fileCSV)](COleDateTime datMeeting)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading