[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: 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)
Date: Wed, 26 Aug 2009 18:36:20 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

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<>"

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

reply via email to

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