0
votes

I am trying to write a managed C++ (aka C++/CLI) library that links to a third-party native C++ library. The third-party library's header files contain constructor definitions that use the nullptr keyword. My managed project fails to compile because the managed compiler defines nullptr to mean the managed null reference, whereas the third-party uses it to mean the native/un-managed null pointer. How can I work around this problem without modifying the third-party library's header file?

For reference on nullptr vs __nullptr, see: https://msdn.microsoft.com/en-us/library/4ex65770.aspx

1
You got a good answer. Taming your #includes is pretty important, striving to minimize the exposure is always important when you interop with foreign code. Just #define _ALLOW_KEYWORD_MACROSHans Passant

1 Answers

5
votes

C++/CLI supports the same preprocessor directives as C++, right? How about this terrible hack:

#define _ALLOW_KEYWORD_MACROS
#define nullptr __nullptr
#include "header.h"
#undef nullptr