Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro for removing the `restrict` keyword when compiling with C++

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.

like image 679
clstaudt Avatar asked Nov 26 '12 14:11

clstaudt


People also ask

Is restrict a keyword in C?

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.

Does C++ have restrict keyword?

restrict is not supported by C++. It is a C only keyword.


1 Answers

#ifdef __cplusplus
#define restrict
#endif

should do it. restrict is not a keyword in C++, so #defineing 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.

like image 138
Daniel Fischer Avatar answered Nov 12 '22 02:11

Daniel Fischer