public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: Lancelot SIX <lsix@lancelotsix.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] gdbsupport: add support for references to checked_static_cast
Date: Wed, 24 May 2023 11:44:28 -0400	[thread overview]
Message-ID: <c554279e-921e-fd67-4258-e7a918b4944c@efficios.com> (raw)
In-Reply-To: <20230524153342.4imwul3zrmvcku6o@octopus>

On 5/24/23 11:33, Lancelot SIX wrote:
> On Wed, May 24, 2023 at 10:51:08AM -0400, Simon Marchi via Gdb-patches wrote:
>> On 5/24/23 09:22, Lancelot SIX wrote:
>>> Hi Simon,
>>>
>>>> diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h
>>>> index bc75244bddd0..7e5a69a6474d 100644
>>>> --- a/gdbsupport/gdb-checked-static-cast.h
>>>> +++ b/gdbsupport/gdb-checked-static-cast.h
>>>> @@ -66,6 +66,22 @@ checked_static_cast (V *v)
>>>>    return result;
>>>>  }
>>>>  
>>>> +/* Same as the above, but to cast from a reference type to another.  */
>>>> +
>>>> +template<typename T, typename V>
>>>> +T
>>>> +checked_static_cast (V &v)
>>>> +{
>>>> +  static_assert (std::is_reference<T>::value, "target must be a reference type");
>>>
>>> Have you considered incorporating this constraint inside
>>> checked_static_cast's signature?  It could look like something like
>>> this:
>>>
>>>     template<typename T, typename V>
>>>     typename std::enable_if<std::is_reference<T>::value, T>::type
>>>     checked_static_cast (V &v)
>>>     {
>>>       ...
>>>     }
>>
>> I like static_assert because it's easier to write and read, and I find
>> that the error messages it produces are easier to understand.  They
>> point you directly to the static_assert line that says "target must be a
>> reference", so you know instantly what is wrong.  With template
>> parameters, you have to decrypt the template substitution message, it's
>> really not obvious.  However...
>>
>>> If T is not a reference, then this template won't be instantiated at all
>>> instead of instantiating it and having an error.  This could allow other
>>> templates / functions to be considered if they matched instead of just
>>> producing an error.
>>
>> ... it seems like there are some pragmatic advantages of using the
>> template parameter method, it's probably the right C++ idiom to use,
>> even if I don't like it much.  We have the gdb::Requires util, which
>> makes it at least more straightforward to read the code:
>>
>>     template<typename T, typename V, typename = gdb::Requires<std::is_reference<T>>>
>>
> 
> C++20 would make this more readable:
> 
>     template<typename T, typename V>
>       requires std::is_reference_v<T>
>     T
>     checked_static_cast (V &v) { ... }
> 
> but this is currently not available to us.

Ah nice, it will be easy to convert gdb::Requires to that.

> With the use of gdb::Requires, the static_assert becomes redundant, but
> It does not hurt to keep it.  If you find it easier to capture the
> intent of the code when reading, I do not mind keeping it.

I would remove it, no point in having both (I think the static_assert
would never trigger anyway).

> Feel free to use my RB
> Reviewed-By: Lancelot Six <lancelot.six@amd.com>

Thanks, I'll add it.  Just waiting for the final feedback from Andrew.

Simon

      reply	other threads:[~2023-05-24 15:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18 20:57 Simon Marchi
2023-05-19 21:31 ` Kevin Buettner
2023-05-23 12:03   ` Simon Marchi
2023-05-24 13:07 ` Andrew Burgess
2023-05-24 14:51   ` Simon Marchi
2023-05-24 18:04     ` Andrew Burgess
2023-05-24 18:54       ` Simon Marchi
2023-05-24 13:22 ` Lancelot SIX
2023-05-24 14:51   ` Simon Marchi
2023-05-24 15:33     ` Lancelot SIX
2023-05-24 15:44       ` Simon Marchi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c554279e-921e-fd67-4258-e7a918b4944c@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gdb-patches@sourceware.org \
    --cc=lsix@lancelotsix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).