attributesgnufunction-attributes

how to specify attribute of GNU C?


In GUN C manual, 6.30 Declaring Attributes of Functions, it states:

You may also specify attributes with __ preceding and following each keyword. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use __noreturn__ instead of noreturn.

Does this mean that user can specify customized attributes? Or what else? I'm definitely confused, could anyone help, or give me some samples?


Solution

  • What it's saying is that a header file might have done:

    #define noreturn 3
    

    If you then try to declare a function as:

    void fatal () __attribute__ ((noreturn));
    

    it will be processed as if you'd written:

    void fatal () __attribute__ ((3));
    

    which is not what you want. So you can write instead:

    void fatal () __attribute__ ((__noreturn__));
    

    This is safe because users are not allowed to define macros with names beginning with two underscores, these names are reserved for the implementation. See What are the rules about using an underscore in a C++ identifier?