Confusing MACRO and enum definition

后端 未结 2 1710
我在风中等你
我在风中等你 2021-01-21 03:40

I was browsing some Route netlink source code.

I wanted to figure out what was the value of RTNLGRP_NEIGH

Source: http://lxr.free-electrons.com/source/include/li

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 04:06

    The value of RTNLGRP_NEIGH will be 3 (it is the fourth enumeration constant: RTNLGRP_NONE has the value 0, RTNLGRP_LINK has the value 1, and RTNLGRP_NOTIFY has the value 2).

    The #define stuff is somewhat weird — it is the sort of thing that's apt to make people want to stop you using the C pre-processor.

    The idea is that it gives you a macro for RTNLGRP_NEIGH that can be tested, but the expansion of the macro is the enumeration constant (spelled the same). There isn't an infinite loop in the expansions because once a macro has been expanded, it is not expanded again while the replacement text is being rescanned.

    So, the upshot is that you can write:

    #ifdef RTNLGRP_NEIGH
       …code using RTNLGRP_NEIGH…
    #endif
    

提交回复
热议问题