开发者

Use of #undef in C++

开发者 https://www.devze.com 2023-03-28 01:25 出处:网络
I am studying a piece of code from GSL libraries and puzzled by few lines in the beginning of a header file.

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 开发者_如何学Go__END_DECLS /* empty */
    #endif


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.


Not really. If another library that you are calling use the same name, that would be already defined.

So, as you cannot define it over another definition, you first call #undef and then #define.

Sometimes it is appropriate to use #ifdef or #ifndef.

0

精彩评论

暂无评论...
验证码 换一张
取 消