"function local static function-object that's initialized by lambda, thread-safe or not?" Code Answer

2

not only is it not thread safe, it doesn't do what you want.

by defining the lambda as static, it captures (by reference) the array passed in the first time it's called. further calls continue to reference the original array, regardless of which array is passed in.

when the first array goes out of scope, further calls will invoke ub, as it now has a dangling reference.

edit: an example http://ideone.com/kccav

note, even if you captured by value, you'd still have a problem because it would still only capture the first time you invoke the function. you wouldn't have the dangling pointer, but it still would only initialize the copy the first time.

By Taslim Oseni on April 4 2022

Answers related to “function local static function-object that's initialized by lambda, thread-safe or not?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.