@@ -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
37203732The attribute can be applied to the implicit ``this`` parameter of a member
37213733function by writing the attribute after the function type:
0 commit comments