From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id hmaQDK4gE2EgGAAAWB0awg (envelope-from ) for ; Tue, 10 Aug 2021 20:58:22 -0400 Received: by simark.ca (Postfix, from userid 112) id 214DC1EDF7; Tue, 10 Aug 2021 20:58:22 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.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 5EFFB1E813 for ; Tue, 10 Aug 2021 20:58:21 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8FD2384800D for ; Wed, 11 Aug 2021 00:58:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8FD2384800D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628643500; bh=pARGux6eVEMlN0E2xxeOJc9nqSSxQlPHWLcI5IcjC/8=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=QAgAzFcQUPxwo5NSJe7NHOvRyWMDkGMV1bTJPUOURz99ekXsDL9DAh23V95WeP4gX 9bWE6+/UD260uYGMlX4mNsfYfs0dPmP7sZfcnfQWyyFyzGjoPPFZzLc41p8DvRD+af +k8gxTBsKa15+TbD3ll3ITB/xoBbnSQGqRNxkycY= Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 3C3EA385DC10 for ; Wed, 11 Aug 2021 00:57:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3C3EA385DC10 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 17B0uhT6001154 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 10 Aug 2021 20:56:49 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 17B0uhT6001154 Received: from [10.0.0.11] (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id B06FC1E813; Tue, 10 Aug 2021 20:56:43 -0400 (EDT) Subject: Re: [PATCH v2 1/4] gdb: Add typesafe getter/setter for cmd_list_element.var To: Lancelot SIX References: <20210808192302.3768766-1-lsix@lancelotsix.com> <20210808192302.3768766-2-lsix@lancelotsix.com> <2cc82d64-f2bd-1e7b-e91c-2b930a8dc75c@polymtl.ca> <20210810215852.vtd2vhnayefcxqi7@Plymouth> Message-ID: <49f18823-1947-ed5a-5d5e-98cf27169588@polymtl.ca> Date: Tue, 10 Aug 2021 20:56:43 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <20210810215852.vtd2vhnayefcxqi7@Plymouth> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Wed, 11 Aug 2021 00:56:44 +0000 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: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Cc: gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" >>> diff --git a/gdb/command.h b/gdb/command.h >>> index baf34401a07..644812c4d46 100644 >>> --- a/gdb/command.h >>> +++ b/gdb/command.h >>> @@ -125,6 +125,164 @@ typedef enum var_types >>> } >>> var_types; >>> >>> +/* Return true if a setting of type VAR_TYPE is backed with type T. >>> + >>> + This function is left without definition intentionally. This template >>> + is specialized for all valid types that are used to back var_types. >>> + Therefore if one tries to instantiate this un-specialized template it >>> + means the T parameter is not a type used to back a var_type and it is most >>> + likely a programming error. */ >>> +template >>> +inline bool var_type_uses (var_types t); >> >> So what will happen if you try to use var_type_uses with an invalid >> template type, link error? Tony suggested using a static_assert to have >> a compile-time error instead, with an explanation. I suppose you would >> define a body for this function and put a static_assert (false) in >> there? > > With a 'standard' (development) build, this translate to compile-time > error. I did not pay enough attention, but I implicitly relied on the > fact that the configure script adds '-Werror' by default. Something > should be in place to fail as early as possible (i.e. compile-time) > whatever compiler options are in use. > > We cannot use a plain 'static_assert (false)' here, it will fail even if > this template is never instantiated. All versions of g++/clang++ I just > tried do trigger consistently: > > $CXX -c -x c++ - < template > void foo () > { static_assert (false, "Invalid instantiation."); } > EOT > > :3:4: error: static_assert failed "Invalid instantiation." > { static_assert (false, "Invalid instantiation."); } > ^ ~~~~~ > 1 error generated. > > I could use something similar to this: > > template > inline bool var_type_uses (var_types var_type ATTRIBUTE_UNUSED) > { > static_assert (sizeof(T) == 0, > "Invalid type used to instantiate 'var_type_uses'. " > "Use one of the types associated with enum var_types."); > return false; > } > > > It is not the prettiest way of writing an assertion, but it is the only > one I am aware of that can work in this situation. > > Would this be OK? I think what you have already is fine. It produces a diagnostic, which is typically error in development builds. And it's not like var_type_uses is meant to be used everywhere, it's used in the internal command code and that's it. I think it's sufficiently clear like that. Simon