On Mon, Mar 04, 2024 at 11:00:18AM +0000, Jonathan Wakely wrote: > On 01/03/24 15:38 -0500, Jason Merrill wrote: > > On 3/1/24 14:24, Marek Polacek wrote: > > > +@smallexample > > > +template <typename T> > > > +[[gnu::no_dangling(std::is_reference_v<T>)]] int foo (T& t) @{ > > > > I think this function should return a reference. > > The condition in the attribute can only ever be true if you call this > function with an explicit template argument list: foo<int&>(i). Is > that intentional?
Not intentional. I just wanted to make it clear that the user can use something like std::is_reference as the attribute argument, but I didn't think about it very long. > And if T is non-const it can't be called with a temporary and so > dangling seems less of a problem for this function anyway, right? Right. > Would it make more sense as something like this? > > template <typename T> > [[gnu::no_dangling(std::is_lvalue_reference_v<T>)]] > decltype(auto) foo(T&& t) { > ... > } > > Or is this getting too complex/subtle for a simple example? I like your example; it's only slightly more complex than the original one and most likely more realistic. I'm pushing the following patch. Thanks! [pushed] doc: update [[gnu::no_dangling]] ...to offer a more realistic example. gcc/ChangeLog: * doc/extend.texi: Update [[gnu::no_dangling]]. --- gcc/doc/extend.texi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index f679c81acf2..df0982fdfda 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -29370,7 +29370,8 @@ Or: @smallexample template <typename T> -[[gnu::no_dangling(std::is_reference_v<T>)]] int& foo (T& t) @{ +[[gnu::no_dangling(std::is_lvalue_reference_v<T>)]] +decltype(auto) foo(T&& t) @{ @dots{} @}; @end smallexample base-commit: 77eb86be8841989651b3150a020dd1a95910cc00 -- 2.44.0