From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id BA75F3858414 for ; Sat, 30 Oct 2021 02:10:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BA75F3858414 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 19U29105027574 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 29 Oct 2021 22:09:06 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 19U29105027574 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)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id B15AB1E940; Fri, 29 Oct 2021 22:09:01 -0400 (EDT) Message-ID: <0edd9c95-f4d8-2034-6fd6-70ec8a03c255@polymtl.ca> Date: Fri, 29 Oct 2021 22:09:01 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: [PATCH 2/2] Improve gdb::array_view ctor from contiguous containers Content-Language: en-US To: Lancelot SIX , gdb-patches@sourceware.org References: <20211020200525.122108-1-lsix@lancelotsix.com> <20211020200525.122108-3-lsix@lancelotsix.com> From: Simon Marchi In-Reply-To: <20211020200525.122108-3-lsix@lancelotsix.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Sat, 30 Oct 2021 02:09:01 +0000 X-Spam-Status: No, score=-5.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: , X-List-Received-Date: Sat, 30 Oct 2021 02:10:10 -0000 On 2021-10-20 16:05, Lancelot SIX via Gdb-patches wrote: > While reading the interface of gdb::array_view, I realized that the > constructor that builds an array_view on top of a contiguous container > (such as std::vector, std::array or even gdb::array_view) can be > miss-used. > > Lets consider the following code sample: > > struct Parent > { > Parent (int a): a { a } {} > int a; > }; > > struct Child : public Parent > { > Child (int a, int b): Parent { a }, b { b } {} > int b; > }; > > std::ostream &operator<< (std::ostream& os, const Parent & p) > { os << "Parent {a=" << p.a << "}"; return os; } > std::ostream &operator<< (std::ostream& os, const Child & p) > { os << "Child {a=" << p.a << ", b=" << p.b << "}"; return os; } > > template > void print_view (const gdb::array_view &p) > { > std::for_each (p.begin (), p.end (), [](const T &e) { std::cout << e << '\n'; }); > } > > Then with the current version of the interface we can write something > like: > > const std::array elts = { > Child {1, 2}, > Child {3, 4}, > Child {5, 6} > }; > print_view (elts); > > This compiles fine and produces the following output: > > Parent {a=1} > Parent {a=2} > Parent {a=3} > > This is obviously wrong. There is nowhere in memory a Parent-like > object for which the A member is 2. This call to print_all > should not compile at all (a call to print_all instead would > however be fine). > > This comes down to the fact that the constructor used in this example > only requires that a Child* is convertible into a Parent*. This > requirement is valid necessary but not sufficient. > > The other constructors of a array_view from data of type U have > stricter requirements on what U should be. It should satisfy that a U* > can be converted to a T*, but they also require that (decayed) T and > (decayed) U are the same. > > This patch proposes to change the constraints on the gdb::array_view > constructor that accepts a Container. It makes it so the > requirements on what U should be are now the same as for the other > constructors. > > Applying this change required minimum adjustment in GDB codebase (two > 'const' addition), which are also included in this patch. > > Tested by rebuilding using GCC-10.3.0 on GNU/Linux. The template part is beyond my understanding, but the explanation makes total sense. Let's leave a bit of time for others that understand this more to respond, but if you don't hear anything in ~1 week, then go ahead and push it. Simon