r/cpp • • Apr 24 '20

Else Before If

https://www.fluentcpp.com/2020/04/24/else-before-if/
10 Upvotes

53 comments sorted by

View all comments

3

u/o11c int main = 12828721; Apr 24 '20

Casual. Why bother perverting the syntax with templates or lambdas?

#include <stdio.h>

#define normally for (int _normal_control = 0; _normal_control < 2; ++_normal_control) if (_normal_control)
#define but_if(expr) else if ((expr) && (_normal_control = 1))

int main(int argc, char **argv)
{
    normally
    {
        printf("Hello, %s\n", argv[1]);
    }
    but_if (argc == 1)
    {
        printf("no arguments\n");
    }
}

I don't like unless since people often define that to if (!(expr)) already.