[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<>"
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
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), (continued)
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), malc, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Jamie Lokier, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), malc, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Jamie Lokier, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code, Markus Armbruster, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code, Anthony Liguori, 2009/08/26
- Re: Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Måns Rullgård, 2009/08/26
- Re: Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Reimar Döffinger, 2009/08/26
- Re: Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Avi Kivity, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense), Avi Kivity, 2009/08/26
- Re: [Qemu-devel] Coding style, C++ compatible code (was Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense),
Jamie Lokier <=
- Re: [Qemu-devel] [PATCH 02/22] eepro100: cast a void * makes no sense, Gerd Hoffmann, 2009/08/24
[Qemu-devel] [PATCH 01/22] eepro100: convert casts to DO_UPCAST(), Juan Quintela, 2009/08/24
[Qemu-devel] [PATCH 03/22] eepro100: Remove unused indirection of PCIDevice, Juan Quintela, 2009/08/24
[Qemu-devel] [PATCH 04/22] es1370: Remove unused indirection of PCIES1370State and ES1370State, Juan Quintela, 2009/08/24
[Qemu-devel] [PATCH 07/22] ne2000: Remove unneeded double indirection of PCINE2000State, Juan Quintela, 2009/08/24
[Qemu-devel] [PATCH 05/22] ne2000: remove casts from void *, Juan Quintela, 2009/08/24