Skip to content

Commit 9c8dab0

Browse files
authored
[clang] Update the lifetimebound example with up-to-date expected warning and change the sample code to be a fully working example (llvm#113437)
Tested the code: https://godbolt.org/z/n5xcq65YM Tested the generated documentation: ![BruDQ2UkTXHA9PE](https://github.com/user-attachments/assets/cf527d1a-ef3b-41f2-84c2-4ca38af16d2d)
1 parent 362273d commit 9c8dab0

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

clang/include/clang/Basic/AttrDocs.td

+20-8
Original file line numberDiff line numberDiff line change
@@ -3702,20 +3702,32 @@ user-declared functions. For example:
37023702

37033703
.. code-block:: c++
37043704

3705+
#include <map>
3706+
#include <string>
3707+
3708+
using namespace std::literals;
3709+
37053710
// Returns m[key] if key is present, or default_value if not.
37063711
template<typename T, typename U>
37073712
const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]],
37083713
const T &key, /* note, not lifetimebound */
3709-
const U &default_value [[clang::lifetimebound]]);
3714+
const U &default_value [[clang::lifetimebound]]) {
3715+
if (auto iter = m.find(key); iter != m.end()) return iter->second;
3716+
else return default_value;
3717+
}
37103718

3711-
std::map<std::string, std::string> m;
3712-
// warning: temporary "bar"s that might be bound to local reference 'val'
3713-
// will be destroyed at the end of the full-expression
3714-
const std::string &val = get_or_default(m, "foo"s, "bar"s);
3719+
int main() {
3720+
std::map<std::string, std::string> m;
3721+
// warning: temporary bound to local reference 'val1' will be destroyed
3722+
// at the end of the full-expression
3723+
const std::string &val1 = get_or_default(m, "foo"s, "bar"s);
37153724

3716-
// No warning in this case.
3717-
std::string def_val = "bar"s;
3718-
const std::string &val = get_or_default(m, "foo"s, def_val);
3725+
// No warning in this case.
3726+
std::string def_val = "bar"s;
3727+
const std::string &val2 = get_or_default(m, "foo"s, def_val);
3728+
3729+
return 0;
3730+
}
37193731

37203732
The attribute can be applied to the implicit ``this`` parameter of a member
37213733
function by writing the attribute after the function type:

0 commit comments

Comments
 (0)