From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MgMQ8-0001C5-I1 for qemu-devel@nongnu.org; Wed, 26 Aug 2009 13:36:28 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MgMQ4-0001A0-Vl for qemu-devel@nongnu.org; Wed, 26 Aug 2009 13:36:28 -0400 Received: from [199.232.76.173] (port=49295 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MgMQ4-00019u-Lq for qemu-devel@nongnu.org; Wed, 26 Aug 2009 13:36:24 -0400 Received: from mail2.shareable.org ([80.68.89.115]:55627) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MgMQ3-00064d-PZ for qemu-devel@nongnu.org; Wed, 26 Aug 2009 13:36:24 -0400 Date: Wed, 26 Aug 2009 18:36:20 +0100 From: Jamie Lokier Subject: Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense) Message-ID: <20090826173620.GB25726@shareable.org> References: <51486eb6860d1680c1bce45e310dcd3aae096f43.1251111439.git.quintela@redhat.com> <4A928DF0.9000106@weilnetz.de> <87tyzxnwvb.fsf@pike.pond.sub.org> <4A953E20.8080806@mail.berlios.de> <4A955AD2.8000500@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <4A955AD2.8000500@redhat.com> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Avi Kivity Cc: Anthony Liguori , Markus Armbruster , qemu-devel@nongnu.org Avi Kivity wrote: > On 08/26/2009 04:52 PM, Stefan Weil wrote: > >Nevertheless, the designers of C++ thought that casts from void * to > >T * were something very important. I don't know the history of their > >decision. I personally think that deriving a data type T from some > >bytes in memory which can contain anything is an operation which is > >worth being documented by the programmer, and this is exactly what > >the cast does. > > Yes. Not really. Adding the cast can hide bugs. If you omit the cast, a C compiler will at least warn, and maybe error, if the original pointer is not "void *", or is "const void *" and you are removing the constness. With the cast, the C compiler will silently let you compile buggy code. In C++, results vary. You are supposed to use a base class pointer, and if you need a cast, you are supposed to use "static_cast<>" anyway. In C++, it's an error without the cast, but it can also be an error with the cast: g++ -Wall test.cc -c -Werror=old-style-cast test.cc: In function ‘S* foo(void*)’: test.cc:5: error: use of old-style cast struct S *foo(void *ptr) { return (struct S *)ptr; } Of course there are dirty tricks to let you omit the "void *" pointer altogether. Let's not go there :-) -- Jamie