@@ -3923,19 +3923,37 @@ def LifetimeCaptureByDocs : Documentation {
3923
3923
let Content = [{
3924
3924
Similar to `lifetimebound`_, the ``lifetime_capture_by(X)`` attribute on a function
3925
3925
parameter or implicit object parameter indicates that objects that are referred to by
3926
- that parameter may also be referred to by the capturing entity ``X``.
3926
+ that parameter may also be referred to by a capturing entity ``X``.
3927
3927
3928
- By default :
3928
+ Below is a list of types of the parameters and what they're considered to refer to :
3929
3929
3930
- - A reference is considered to refer to its referenced object.
3931
- - A pointer is considered to refer to its pointee.
3930
+ - A reference param is considered to refer to its referenced object.
3931
+ - A pointer param is considered to refer to its pointee.
3932
3932
- A ``std::initializer_list<T>`` is considered to refer to its underlying array.
3933
3933
- Aggregates (arrays and simple ``struct``\s) are considered to refer to all
3934
3934
objects that their transitive subobjects refer to.
3935
- - View types (types annotated with ``[[gsl::Pointer()]]``) are considered to refer
3936
- to its pointee. This holds true even if the view type appears as a reference.
3937
- For example, both ``std::string_view`` and ``const std::string_view &`` are
3938
- considered to refer to a ``std::string``.
3935
+ - View type param (type annotated with ``[[gsl::Pointer()]]``) is considered to refer
3936
+ to its pointee. This holds true even if the view type appears as a reference
3937
+ in the parameter. For example, both ``std::string_view`` and
3938
+ ``const std::string_view &`` are considered to refer to a ``std::string``.
3939
+
3940
+ Clang would diagnose when a temporary object is used as an argument to such an
3941
+ annotated parameter.
3942
+ In this case, the capturing entity ``X`` could capture a dangling reference to this
3943
+ temporary object.
3944
+
3945
+ .. code-block:: c++
3946
+
3947
+ void addToSet(std::string_view a [[clang::lifetime_capture_by(s)]], std::set<std::string_view>& s) {
3948
+ s.insert(a);
3949
+ }
3950
+ void use() {
3951
+ std::set<std::string_view> s;
3952
+ addToSet(std::string(), s); // Warning: object whose reference is captured by 's' will be destroyed at the end of the full-expression.
3953
+ // ^^^^^^^^^^^^^
3954
+ std::string local;
3955
+ addToSet(local, s); // Ok.
3956
+ }
3939
3957
3940
3958
The capturing entity ``X`` can be one of the following:
3941
3959
@@ -3990,21 +4008,6 @@ The attribute supports specifying more than one capturing entities:
3990
4008
s2.insert(a);
3991
4009
}
3992
4010
3993
- Currently Clang would diagnose when a temporary is used as an argument to a
3994
- parameter whose reference is captured.
3995
-
3996
- .. code-block:: c++
3997
-
3998
- void addToSet(std::string_view a [[clang::lifetime_capture_by(s)]], std::set<std::string_view>& s) {
3999
- s.insert(a);
4000
- }
4001
- void use() {
4002
- std::set<std::string_view> s;
4003
- addToSet(std::string(), s); // Warning: object whose reference is captured by 's' will be destroyed at the end of the full-expression.
4004
- std::string local;
4005
- addToSet(local, s); // Ok.
4006
- }
4007
-
4008
4011
.. _`lifetimebound`: https://clang.llvm.org/docs/AttributeReference.html#lifetimebound
4009
4012
}];
4010
4013
}
0 commit comments