Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sds.h compatible for C++ #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

franzflasch
Copy link

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.

@@ -80,7 +80,7 @@ struct __attribute__ ((__packed__)) sdshdr64 {
#define SDS_TYPE_64 4
#define SDS_TYPE_MASK 7
#define SDS_TYPE_BITS 3
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T*)((s)-(sizeof(struct sdshdr##T)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not stick those in an #ifdef,

#ifdef __cplusplus
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T*)((s)-(sizeof(struct sdshdr##T)));
#else
#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T)));
#endif

Source: https://www.cprogramming.com/reference/preprocessor/ifdef.html

@franzflasch
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants