-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Description
Please be patient reading all examples below - root of the evil hidden in the fact that std::pair<const A, B> not the same as std::pair<A, B> but the first one might be implicitly converted to the second one. Such implicit conversion might lead to hidden UB if not carefully coded.
Assume we have a proj lambda like this:
const auto proj =
[](const std::pair<std::string, int>& node [[clang::lifetimebound]])
-> const std::pair<std::string, int>& {
return node;
};
Let's call this lambda directly by creating the problem with dangling reference artifically:
std::pair<const std::string, int> aa;
const auto& a = proj(aa);
Then we will watch warning: temporary bound to local reference 'a' will be destroyed at the end of the full-expression [-Wdangling] emited - that's fine.
Now let's create conditions under which the function will be called outside of user code, but the problem with the dangling reference will still be relevant:
std::map<std::string, int> counters = { {"hello", 5}, {"world", 5} };
for (const auto& node : counters | std::views::transform(proj)) {
(void)node; // dangling
}
And here we will not see that warning emited - that's the problem that I am creating this issue about and which should be fixed.
The full snippet here: https://godbolt.org/z/1Yaj1s6Wf
Metadata
Metadata
Assignees
Labels
Type
Projects
Status