logoalt Hacker News

troglo-bytetoday at 11:43 AM4 repliesview on HN

    #include <iostream>
    #define RemParens_(VA) RemParens__(VA)
    #define RemParens__(VA) RemParens___ VA
    #define RemParens___(...) __VA_ARGS__
    #define DoConcat_(A,B) DoConcat__(A,B)
    #define DoConcat__(A,B) A##B
    #define defer(BODY) struct DoConcat_(Defer,__LINE__) { ~DoConcat_(Defer,__LINE__)() { RemParens_(BODY) } } DoConcat_(_deferrer,__LINE__)

    int main() {
        {
            defer(( std::cout << "Hello World" << std::endl; ));
            std::cout << "This goes first" << std::endl;
        }
    }

Replies

rezonanttoday at 11:57 AM

Why would that be preferable to just using an RAII style scope_exit with a lambda

show 1 reply
pelorattoday at 2:41 PM

And here I thought we were trying to finally kill off pre-processor macros.

chuckadamstoday at 1:01 PM

"We have syntax macros at home"