I need to include some headers originally written in C in a C++ project. In the header files, the restrict
keyword is used, which leads to a syntax error for C++.
I am looking for a preprocessor macro which checks whether I am compiling with a C++ compiler and removes the restrict
keyword in this case.
In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.
restrict is not supported by C++. It is a C only keyword.
#ifdef __cplusplus
#define restrict
#endif
should do it. restrict
is not a keyword in C++, so #define
ing it to nothing is unproblematic there.
Or, as Arne Mertz suggested, better still, have
extern "C" {
#define restrict
// include C headers here
#undef restrict
}
where you include the C headers in your C++ source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With