[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [patch][c++] fix "invalid conversion from ‘void*’ to ‘char*’"
- From: Coda Highland <chighland@...>
- Date: Tue, 28 Jun 2016 12:40:46 -0700
On Tue, Jun 28, 2016 at 12:38 PM, Christian N. <cn00@gmx.at> wrote:
> On 2016-06-28 21:32 +0200, Sean Conner wrote:
>>
>> It was thus said that the Great Roberto Ierusalimschy once stated:
>>>>
>>>> What I really meant was that with that macro you could perform the
>>>> cast on the pointer returned from malloc only when compiling as C++,
>>>> and avoid the cast in C, in order to "conform" to C best practices
>>>> [1] that advice against casting that pointer (as opposed as what you
>>>> should do in C++), as Sean said.
>>>
>>>
>>> Let us see [1]:
>>>
>>> * "It is unnecessary" - so are comments, identation, etc.
>>>
>>> * "It can hide an error, if you forgot to include <stdlib.h>." - This
>>> could be a valid argument 20 years ago (K&R C). If you do not use
>>> something like -Wstrict-prototypes and -Wmissing-prototypes, something
>>> is very wrong, as every single function call in your program could hide
>>> errors.
>>>
>>> * "It adds clutter to the code" - A macro would add even more "clutter".
>>>
>>> * "It makes you repeat yourself" - So does a macro.
>>
>>
>> So if you are adding the casts because C++ requires them, then you
>> should
>> also remove all NULLs since C++ practically rejects those.
>>
>> -spc (and to further compound the issue, you should therefore define
>> nullptr to be NULL when compiling with a C compiler, because you
>> know ... C is a second class citizen ... )
>>
>>
>
> Edge cases with overload resolution aside (which Lua, being written in the C
> subset of C++, is not affected from), there is no problem with using NULL in
> C++. In fact, before C++11 introduced nullptr, it was common practice, so I
> wouldn't even say that NULL is "C style".
>
Depends on who you talk to. The style guides I followed in C++ said to
use a numeric literal 0 instead of the NULL macro. (This might be in
part because the NULL macro is sometimes defined as (void*)0 which
could trigger cast warnings in some compilers.)
/s/ Adam