public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Lancelot SIX <lsix@lancelotsix.com>
To: Simon Marchi <simon.marchi@efficios.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] gdbsupport: add support for references to checked_static_cast
Date: Wed, 24 May 2023 14:22:32 +0100	[thread overview]
Message-ID: <20230524132157.rhdftbdnjckyzdnt@octopus> (raw)
In-Reply-To: <20230518205737.403656-1-simon.marchi@efficios.com>

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)
    {
      ...
    }

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.

I do agree that this can end-up a bit more difficult to read for someone
not used to read this kind of code.

Best,
Lancelot.

> +
> +  using T_no_R = typename std::remove_reference<T>::type;
> +  using T_P = typename std::add_pointer<T_no_R>::type;
> +
> +  using V_no_R = typename std::remove_reference<V>::type;
> +
> +  return *checked_static_cast<T_P, V_no_R> (&v);
> +}
> +
>  }
>  
>  #endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */
> 
> base-commit: c96452ad168cf42ad42f0d57214dddb38d5fae88
> -- 
> 2.40.1
> 

  parent reply	other threads:[~2023-05-24 13:22 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 [this message]
2023-05-24 14:51   ` Simon Marchi
2023-05-24 15:33     ` Lancelot SIX
2023-05-24 15:44       ` Simon Marchi

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=20230524132157.rhdftbdnjckyzdnt@octopus \
    --to=lsix@lancelotsix.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.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).