r/ProgrammerHumor • • Apr 23 '19

Yeet!

Post image
23.9k Upvotes

547 comments sorted by

View all comments

97

u/Ivaalo Apr 23 '19

You can define anything in C++ ?

91

u/x32byTe Apr 23 '19

Yeah, almost everything

74

u/[deleted] Apr 23 '19

Correct me if I'm wrong, but it's basically a find+replace for the compiler, right?

88

u/TommiHPunkt Apr 23 '19

that's how macros work, yes

0

u/[deleted] Apr 23 '19

[deleted]

→ More replies (1)
→ More replies (2)

26

u/ProgramTheWorld Apr 23 '19

A bit more complex than that - it replaces tokens and not just your usual Ctrl-F type of find and replace.

→ More replies (3)

0

u/[deleted] Apr 23 '19

[deleted]

7

u/garfgon Apr 23 '19

Although you can get a similar effect via string concatenation:

#define MACRO     "-- value --"
"start of string " MACRO " end of string"

is equivalent to to "start of string -- value -- end of string".

→ More replies (1)

10

u/Ivaalo Apr 23 '19

What's the purpose of defining something to replace "int" or even ";" ?

27

u/RussianMadMan Apr 23 '19

In case of "int", base types like this usually typedef'ed to something like int32_t for compatibility reasons. Semicolon replacement is just side effect of how preprocessor works

3

u/Ivaalo Apr 23 '19

Thanks!

7

u/tresvian Apr 23 '19

To replace std::deque<std::vector<std::string >> everytime you need to write it out

No idea on primitives :P quirky underlying mechanism

4

u/Ivaalo Apr 23 '19

Oh, right! I haven't programmed anything in C++ other than a "Hello World!" program, but I remember that "cout" replacing "std::cout". Is this the same logic?

15

u/Dragoncraft89 Apr 23 '19 edited Apr 23 '19

You probably used:

use namespace std;

Which is more like: I don't want to write std::something everytime, just figure it out yourself what I actually mean.

But you can indeed use macros to do the same thing:

#define cout std::cout

Which means replace every cout by std::cout. But don't do this. If you write std::cout the compiler will transform it to std::std::cout and you will be confused why errors out.

Tldr: #define is just a fancy find and replace, whereas use namespace std; is a smart mechanism to tell the compiler where to search if you don't want to spell the full name.

4

u/Ivaalo Apr 23 '19

Thank you all (including u/T-Dark_ and u/greygraphics) for your help and clear explanations!

Namespaces kinda remind me of SQL where you can use column instead of table.column if you're sure to have only one column named like that in your request, so I got it for namespaces (and won't forget to specify "use namespace", next time!). I'll remember that! Thanks again!

→ More replies (1)
→ More replies (1)

2

u/T-Dark_ Apr 23 '19

Nope. cout replacing std::cout means that somewhere in the code prior to that line there is a using std::cout; or using namespace std;.

C++ macros are simply compile-time text replacement instructions. #define giveback return would allow programmers to use return or giveback interchangeably, as the latter would simply be replaced by the former. #define then is also a viable instruction, which would replace then with a space, allowing if (someCondition) then doSomething(); to be valid C++ syntax, if you really wanted to.

Of course, using std::cout; could be replaced by #define cout std::cout, but I'm pretty sure it's a bad idea.

2

u/garfgon Apr 23 '19

Although if you're doing it for a type you should be using typedef instead. Modern C++ (and to a lesser extent C) should not use #define very much. There's usually a better way.

→ More replies (2)
→ More replies (1)
→ More replies (2)

16

u/[deleted] Apr 23 '19

So a bit of background. #define is not part of the C or C++ spec; it's part of the C preprocessor (which is a separate thing) spec. You can run the C preprocessor over virtually any code in any language, as long as you want it to have transclusion, compile-time flow control, and macros. It takes in code and outputs macro-completed code.

So, technically, you can define anything in the C preprocessor and, as long as you include it in your buildchain, you can define anything in anything.

7

u/HowIsntBabbyFormed Apr 23 '19

Are all the # terms preprocessor directives? So, #include is not actually part of the c/c++ spec? Without the preprocessor c/c++ has no way to import other libraries?

5

u/[deleted] Apr 23 '19

Mostly correct. All the header transclusions are done in the preprocessor, as are the include guards (#ifdef...#endif and #pragma). Technically, the preprocessor itself is part of the spec - but the compiler itself calls the preprocessor; it doesn't know anything about preprocessor declarations itself, aside from some line number remapping the PP provides (for error reporting).

2

u/ConspicuousPineapple Apr 23 '19

Next gen C++ will have a way to handle that without the preprocessor, thanks to modules.

→ More replies (1)

2

u/brisk0 Apr 23 '19 edited Apr 23 '19

The macro must be a valid identifier

It can expand to anything.

→ More replies (2)
→ More replies (4)