antirez / sds

Simple Dynamic Strings library for C
BSD 2-Clause "Simplified" License
4.89k stars 473 forks source link

Make sds.h compatible for C++ #125

Open franzflasch opened 4 years ago

franzflasch commented 4 years ago

I know that sds is a library solely intended for C but I happened to include the header "sds.h" in a C++ application, I don't want to go into details why I needed to do this. However, when compiling the C++ application I get some of these errors:

sds.h:83:49: error: invalid conversion from ‘void*’ to ‘sdshdr8*’ [-fpermissive]
 #define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));                 
                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  

A minor change in the header would fix this issue. I don't think that it breaks anything, but I think you @antirez know best if it causes side-effects or not.

I've tested this change with in my Application and it works fine in C and C++ so far.

franzflasch commented 4 years ago

Is there actually any reason for using a (void) cast instead of just using (struct sdshdr##T)? I find #ifdefs rather ugly and I don't see any drawback of casting to (struct sdshdr##T*), but maybe there is some case which I don't see...