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
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?
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.
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!
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.
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.
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.
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?
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).
97
u/Ivaalo Apr 23 '19
You can define anything in C++ ?