@@ -3702,20 +3702,32 @@ user-declared functions. For example:
3702
3702
3703
3703
.. code-block:: c++
3704
3704
3705
+ #include <map>
3706
+ #include <string>
3707
+
3708
+ using namespace std::literals;
3709
+
3705
3710
// Returns m[key] if key is present, or default_value if not.
3706
3711
template<typename T, typename U>
3707
3712
const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]],
3708
3713
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
+ }
3710
3718
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);
3715
3724
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
+ }
3719
3731
3720
3732
The attribute can be applied to the implicit ``this`` parameter of a member
3721
3733
function by writing the attribute after the function type:
0 commit comments