简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM