Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any downside to having a #define nullptr in C code?

Tags:

c++

c

I am writing some header files that will be included by C++ code and also included by C code. I would prefer for the header source code to be consistent and always use "nullptr" rather than NULL (or the integer constant 0) in these headers.

This brings up the possibility of adding a piece of code such as:

#ifndef __cplusplus
#define nullptr 0      /* or ((void *)0) */
#endif

Or alternatively:

#include <stdio.h>
#ifndef __cplusplus
#define nullptr NULL
#endif

Edit: Or from Ben Voigt's suggestion:

#ifndef __cplusplus
static const void *nullptr = 0;
#endif

The question is, what is the downside to doing this? Is there some reason why nullptr was added to C++ but not to C? This seems like something that would make code more consistent and readable when C++ and C have to inter-operate.

Edit: a sample header

#ifdef __cplusplus
extern "C" {
#endif

extern struct MyStruct *globalMyStruct;
extern int myFunc(struct MyStruct *ptr,int arg);

#ifdef __cplusplus
}
#endif

#define MYMACRO(x) ((globalMyStruct!=nullptr) ? myFunc(globalMyStruct,(x)) : -1)

Even if MyStruct is a C++ object, then it is legal for C code to include the above header file and use the MYMACRO definition. I want to include the above header code in both C and C++ files. But if I include the above header from a C file, then it won't compile due to the use of nullptr.

It is perfectly valid for C code to contain pointers to C++ objects, and those pointers can be passed as arguments to functions (and perhaps those functions are written in C++). This is just one way C++ and C can inter-operate.

like image 741
deltamind106 Avatar asked Sep 11 '17 16:09

deltamind106


People also ask

What's the downside meaning?

The downside is defined as the negative aspects or the disadvantages of something. An example of the downside of home ownership is the costs and expenses with home upkeep. noun. The lower side or portion. noun.

What does see you on the upside mean?

Considering the positive, beneficial, advantageous, etc., aspects of a situation, especially one that is or would be otherwise negative, detrimental, or disadvantageous.

What are the pros and cons of having?

Definition of pros and cons 1 : arguments for and against —often + of Congress weighed the pros and cons of the new tax plan. 2 : good points and bad points Each technology has its pros and cons.


1 Answers

Two downsides off the top of my head:

  • NULL/0 and nullptr have different behavior (as explained here) You wouldn't want nullptr(macro) and nullptr(language feature) to have different behavior even though they look the same, would you?
  • Macros should be avoided (as much as possible in C, anyway...)

Edit: Something I do in older versions of C++ (but not C):

#if __cplusplus >= 201103L || (__cplusplus < 200000 && __cplusplus > 199711L)
//use C++ 11 nullptr
#else
    struct nullptr_t
    {
        template <class T>
            operator T* (){return (T*)0;}
    }nullptr;
#endif

It mimicks the behavior of nullptr without being a macro.

like image 92
Alexander Ivanov Avatar answered Sep 19 '22 21:09

Alexander Ivanov