Hacker News new | past | comments | ask | show | jobs | submit login

That's interesting -- I was wondering in which cases typedef changes the parse tree, and came across a few [1]:

    a (b);      /* function call or declaration */
    a * b;      /* multiplication or declaration */
    f((a) * b); /* multiplication or deref and cast */
> With one further change, namely deleting the production typedef-name: identifier and making typedef-name a terminal symbol, this grammar is acceptable to the YACC parser-generator.

[1] http://eli.thegreenplace.net/2007/11/24/the-context-sensitiv...




С++ takes it all the way to 11 with templates. Here's a program that is parsed differently depending on whether pointers are 32-bit or 64-bit:

    template<size_t N = sizeof(void*)> struct a;

    template<> struct a<4> {
        enum { b };
    };

    template<> struct a<8> {
        template<int> struct b {};
    };

    enum { c, d };

    int main() {
        a<>::b<c>d;
        d;
    }
Depending on which instantiation is used, the first line of main is either a variable declaration, or two operators < and > applied in sequence.

This is especially fun to deal with for C++ IDEs that support semantic highlighting (i.e. typenames are in a different color etc). If I remember correctly, the first one that could handle this right was VS 2012 - it only took 14 years after ISO C++ standard was released...




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

Search: