r/cpp • • Aug 08 '20

Create a fully functioning command line interface with 1.5 lines of code (wo. include statement)

https://github.com/kongaskristjan/fire-hpp
230 Upvotes

62 comments sorted by

View all comments

Show parent comments

11

u/kongaskristjan Aug 08 '20

Though having it's perks, #pragma once is non-standard according to wikipedia.

0

u/[deleted] Aug 08 '20

[deleted]

9

u/kongaskristjan Aug 08 '20

Well, in that case everyone would need to write something like that to the end of their main.cpp:

int main(int argc, const char ** argv) {
    init_and_run(argc, argv, fired_main, true);
    return fired_main();
}

I generally agree that macros should be avoided because of all their complex errors and unintuitive behaviour, but here it's a really simple one that's hard to misuse.

1

u/F54280 Aug 08 '20

That sounds perfectly fine boilerplate to me. Sure, it would be better if it was smaller, but you only have 3 lines: main, init and return.

int main(int argc, const char ** argv) {
    fire_init(argc, argv, my_main);
    return my_main();
}

That honestly seems pretty logical, and not too magic (fire_init initialize some global based on argc, argv and the signature of my_main, and the default arguments from my_main fetch from that global -- it then raises the question of why not getting rid of the global itself).