Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of #undef in C++

I am studying a piece of code from GSL libraries and puzzled by few lines in the beginning of a header file. I understand what #undef, #ifdef and etc. do but what I don't understand is why did they basically reset the definition of the _BEGIN_DECLS and then go on and define it again? Technically, there shouldn't be any previous definitions, right? I mean, those things are static and are not subject to changes. Anyway, here is the excerpt from the code:

    #undef __BEGIN_DECLS
    #undef __END_DECLS
    #ifdef __cplusplus
    # define __BEGIN_DECLS extern "C" {
    # define __END_DECLS }
    #else
    # define __BEGIN_DECLS /* empty */
    # define __END_DECLS /* empty */
    #endif
like image 268
GKED Avatar asked Feb 03 '23 15:02

GKED


1 Answers

You are not allowed to #define a macro that is already defined unless the parameter lists and replacement lists are identical.

If __BEGIN_DECLS was previously defined to be replaced by something other than extern "C" {, the #define __BEGIN_DECLS extern "C" { would be invalid and the program would be ill-formed.

Technically, there shouldn't be any previous definitions, right?

There could have been, sure.

like image 175
James McNellis Avatar answered Feb 05 '23 05:02

James McNellis