Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

after

  #define if(X) LET(_it,X) if (_it)
it would be a macro call


I'm fairly certain that shadowing a keyword with a macro is forbidden by the standard (or invokes UB?) but this will still work for most compilers, provided LET is defined reasonably. Hm, something like

     #define LET(name, X) \
       for (int let_once_=1, name=(X); \
            let_once_; \
            let_once_=0)
may be reasonable, though tokenpasting in __LINE__ for good measure might be necessary for nesting.


Shadowing keywords is perfectly valid, although I believe you can't shadow keywords in standard headers so that compiler writers don't need to worry about it. It gives rise to some "useful" C/C++ features that should never be used, like if you want to access private members in foo.cpp, do

     #define private public 
     #include "foo.cpp"
     #undef private


I can imagine some insidious menace adding this to the top of a file:

    #define public protected


I once someone jokingly do (the second only always works in C, not C++)

    #define else
    #define struct union
Evil!




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: