Skip to content

Commit

Permalink
[clang] Update the lifetimebound example with up-to-date expected war…
Browse files Browse the repository at this point in the history
…ning and change the sample code to be a fully working example (#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)
  • Loading branch information
bricknerb authored Oct 30, 2024
1 parent 362273d commit 9c8dab0
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -3702,20 +3702,32 @@ user-declared functions. For example:

.. code-block:: c++

#include <map>
#include <string>

using namespace std::literals;

// Returns m[key] if key is present, or default_value if not.
template<typename T, typename U>
const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]],
const T &key, /* note, not lifetimebound */
const U &default_value [[clang::lifetimebound]]);
const U &default_value [[clang::lifetimebound]]) {
if (auto iter = m.find(key); iter != m.end()) return iter->second;
else return default_value;
}

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

// No warning in this case.
std::string def_val = "bar"s;
const std::string &val = get_or_default(m, "foo"s, def_val);
// No warning in this case.
std::string def_val = "bar"s;
const std::string &val2 = get_or_default(m, "foo"s, def_val);

return 0;
}

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

0 comments on commit 9c8dab0

Please sign in to comment.