[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel]

From: Avi Kivity
Subject: Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense)
Date: Wed, 26 Aug 2009 18:54:58 +0300
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20090814 Fedora/3.0-2.6.b3.fc11 Lightning/1.0pre Thunderbird/3.0b3

On 08/26/2009 04:52 PM, Stefan Weil wrote:
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.

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).

You are not alone.

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.


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).

It does work in C++.

* 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.

It's often needed, for example to have a struct T * in T.

error compiling committee.c: too many arguments to function

reply via email to

[Prev in Thread] Current Thread [Next in Thread]