Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MgKIs-0004IB-8a for qemu-devel@nongnu.org; Wed, 26 Aug 2009 11:20:50 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MgKIn-0004Ao-4I for qemu-devel@nongnu.org; Wed, 26 Aug 2009 11:20:49 -0400 Received: from [199.232.76.173] (port=40834 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MgKIm-0004AV-UK for qemu-devel@nongnu.org; Wed, 26 Aug 2009 11:20:44 -0400 Received: from lo.gmane.org ([80.91.229.12]:39673) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MgKIm-00033W-7U for qemu-devel@nongnu.org; Wed, 26 Aug 2009 11:20:44 -0400 Received: from list by lo.gmane.org with local (Exim 4.50) id 1MgKIj-0002L0-0r for qemu-devel@nongnu.org; Wed, 26 Aug 2009 17:20:41 +0200 Received: from unicorn.mansr.com ([78.86.181.103]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 26 Aug 2009 17:20:41 +0200 Received: from mans by unicorn.mansr.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 26 Aug 2009 17:20:41 +0200 From: =?iso-8859-1?Q?M=E5ns_Rullg=E5rd?= Subject: Re: Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense) Date: Wed, 26 Aug 2009 16:20:14 +0100 Message-ID: References: <51486eb6860d1680c1bce45e310dcd3aae096f43.1251111439.git.quintela@redhat.com> <4A928DF0.9000106@weilnetz.de> <87tyzxnwvb.fsf@pike.pond.sub.org> <4A953E20.8080806@mail.berlios.de> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: news List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Stefan Weil writes: > Markus Armbruster schrieb: >> Stefan Weil writes: >> >>> Juan Quintela schrieb: >>>> Signed-off-by: Juan Quintela >>>> --- >>>> hw/eepro100.c | 6 +++--- >>>> 1 files changed, 3 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/hw/eepro100.c b/hw/eepro100.c >>>> index 0031d36..09083c2 100644 >>>> --- a/hw/eepro100.c >>>> +++ b/hw/eepro100.c >>>> @@ -507,7 +507,7 @@ static void nic_selective_reset(EEPRO100State * s) >>>> >>>> static void nic_reset(void *opaque) >>>> { >>>> - EEPRO100State *s = (EEPRO100State *) opaque; >>>> + EEPRO100State *s = opaque; >>>> logout("%p\n", s); >>>> static int first; >>>> if (!first) { >>>> @@ -1544,7 +1544,7 @@ static ssize_t nic_receive(VLANClientState >>>> *vc, const uint8_t * buf, size_t size >>>> >>>> static int nic_load(QEMUFile * f, void *opaque, int version_id) >>>> { >>>> - EEPRO100State *s = (EEPRO100State *) opaque; >>>> + EEPRO100State *s = opaque; >>>> int i; >>>> int ret; >>>> >>>> @@ -1634,7 +1634,7 @@ static int nic_load(QEMUFile * f, void >>>> *opaque, int version_id) >>>> >>>> static void nic_save(QEMUFile * f, void *opaque) >>>> { >>>> - EEPRO100State *s = (EEPRO100State *) opaque; >>>> + EEPRO100State *s = opaque; >>>> int i; >>>> >>>> if (s->pci_dev) >>>> >>> I wrote these type casts, and I think they make sense. >>> In C++ code, they are even mandatory. >> >> Yes, but this isn't C++. >> >>> I think the arguments why C++ requires this kind of >>> type casts apply to C code (like in Qemu) as well. >>> >>> If it is possible with no or very litte efforts to write >>> code which is C and C++ compatible, I prefer to do so. >> >> I respectfully disagree. Casts from "void *" to "T *" are pure noise. >> Getting into the habit of writing noise casts runs the risk of silencing >> warnings that flag real type errors. > > Hello > > Do you only disagree with my first sentence or with both sentences? > > Currently, I seem to be alone with my opinion (at least in qemu-devel). > 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 don't know the history of C++ either. What I do know is that the void* type was added to the C language precisely to *avoid* such casts. The original K&R C used char* as return type for malloc() etc, and this of course required a cast. > 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. The declaration and assignment already make that perfectly clear. The cast is at best noise, and often hides a real error. > My main reason why I try to write portable code is my personal > experience with code reuse and the future development of compilers. > It is quite possible that some day C compilers will require > type casts like C++ compilers do today. Any syntax or semantics can potentially change in the future. If *I* were in charge, I'd make frivolous casts an *error*. > And even today I want to be able to share C code from QEMU with > program code written in C++ (which makes a large part of my > business work). That's your problem. Please don't bring it here. > Let me give one more C/C++ example. Today, many data structures > are declared like this: typedef struct T { ... } T; > There is nothing wrong with it, but it can be improved in > several ways: > > * The declaration does not work with C++ (yes, I know that many > programmers are not interested in C++ compatibility for QEMU). Nor does it work in Fortran or COBOL. > * The declaration allows variable declarations using struct T var; > or just T var; (which is the QEMU style). I think a declaration > which does not enforce the correct style is less good. > > * The declaration contains noise (bad argument, but many people > like speaking of noise) :-) > > Why don't we declare structures like this: typedef struct { ... } T;? > I suggest this to be the new coding style for structure declarations > because it is shorter, C++ compatible and unambiguous. In my personal projects, I never use typedefs for structs. Typing those 5 characters isn't much of a burden, and it makes it unambiguously clear that something is a struct and not something else. It even works in C++, should you care. C is not C++, even though much of the syntax is confusingly similar. Trying to shoehorn the same coding style onto both is a mistake, and does more harm than good whichever of the languages you are writing in. -- Måns Rullgård mans@mansr.com