From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id iF8JKnEubmSM4xIAWB0awg (envelope-from ) for ; Wed, 24 May 2023 11:34:09 -0400 Received: by simark.ca (Postfix, from userid 112) id 99F411E11E; Wed, 24 May 2023 11:34:09 -0400 (EDT) Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=rbbHgy+6; dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 2386D1E0D4 for ; Wed, 24 May 2023 11:34:09 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82C713857704 for ; Wed, 24 May 2023 15:34:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82C713857704 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1684942448; bh=/nTa4weVJbAgCbJuVXfxYuw/kDs98mMQVAluHKkVB9k=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=rbbHgy+6IDZIvG8IU79eoOFr5aKItPGB87cVKnLF5RkfhGT17Wks65/Ig4f4DS4j4 ZrRc6pqLC45uJHb2cD8CxdKM9VcsDTNJTAckXJ57QkafSgZ1nSvtd5HbC71dQowzpC a9N+vXcPDSbNkJLVP3g0HW/Equ40AoajjOJhAl1E= Received: from lndn.lancelotsix.com (vps-42846194.vps.ovh.net [IPv6:2001:41d0:801:2000::2400]) by sourceware.org (Postfix) with ESMTPS id EE99A3858D28 for ; Wed, 24 May 2023 15:33:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE99A3858D28 Received: from octopus (cust120-dsl54.idnet.net [212.69.54.120]) by lndn.lancelotsix.com (Postfix) with ESMTPSA id E2EB48055C; Wed, 24 May 2023 15:33:47 +0000 (UTC) Date: Wed, 24 May 2023 16:33:42 +0100 To: Simon Marchi Cc: gdb-patches@sourceware.org Subject: Re: [PATCH] gdbsupport: add support for references to checked_static_cast Message-ID: <20230524153342.4imwul3zrmvcku6o@octopus> References: <20230518205737.403656-1-simon.marchi@efficios.com> <20230524132157.rhdftbdnjckyzdnt@octopus> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.11 (lndn.lancelotsix.com [0.0.0.0]); Wed, 24 May 2023 15:33:47 +0000 (UTC) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Lancelot SIX via Gdb-patches Reply-To: Lancelot SIX Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" 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 > >> +T > >> +checked_static_cast (V &v) > >> +{ > >> + static_assert (std::is_reference::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 std::enable_if::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>> > C++20 would make this more readable: template requires std::is_reference_v T checked_static_cast (V &v) { ... } but this is currently not available to us. 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. Feel free to use my RB Reviewed-By: Lancelot Six Lancelot. > I'll push it with that change. Should I have your RB? > > Simon