Rewrite keyword

Keeping in mind what register

is a keyword and we can use to refer to functions / methods (I really miss it), we cannot do that. But I wonder if it is dangerous to rewrite this keyword?

#define register ...

      

Are there any side effects?

+3


source to share


3 answers


C ++ says in 17.6.4.3.1[macro.names]/2

The translation unit must not contain the names #define or #undef, lexically identical to the keywords

although this falls under

This section describes the restrictions on C ++ programs that use the C ++ Standard Library features.



C says in 7.1.2/4

The program must not have any macros with names lexically identical to the keywords currently defined before the header is included or when any macro defined in the header is expanded

so in C you can put this at the end #include

(Edit: as pointed out in the comments, even in C, using a macro that comes from the standard library header after yours #define

is formally undefined as it might use that keyword)

+9


source


Even if it wasn't forbidden by the spec, it will still break things if you do.

Next source:

#define foo register int a;

#define register static

int main()
{
    foo
    return 0;
}

      



creates this when run through the MSVC compiler using the / P switch

#line 1 "test.cpp"

int main()
{
    static int a;
    return 0;
}

      

This means that even if your #define follows any #defines in the headers that might include the word "register" in their extensions, due to C / C ++ macro expansion happening, you will break those macros.

+2


source


Even this will be allowed and / or works does not . This makes not only your program unreadable because it register

will be confusing (and this is very bad coding style), but all the programs that have #include

your header file!

Rather, use a related name ("Registration" or similar).

Avoid macros as much as possible. In particular, stupid macros like MAX

or MIN

(which are defined in some library headers). They are confusing and can cause serious headaches because the compiler is not aware of them, so your inadvertent use MAX

as an identifier causes confusing compiler messages.

+1


source







All Articles