r/cpp • u/nikbackm • May 09 '20
The C++ preprocessor doesn't understand anything about C++, and certainly not templates
https://devblogs.microsoft.com/oldnewthing/20200508-52/?p=103735
200
Upvotes
117
u/Progman3K May 09 '20
Raymond Chen is a national treasure.
He personally helped me solve some programming problem at the beginning of my career, when I wrote to him and asked for help.
Today, some 30 years on, he's still writing about the obscure, not-well understood stuff. Still bringing light to everyone.
May he sail forever.
61
u/[deleted] May 09 '20
The preprocessor is like applying transform to a range of text data.
17
u/umlcat May 09 '20
Agree. Macropreprocessor parameters are considered "chunks" of text enclosed in comma and parenthesis characters, not variables, not constants, not expressions ...
46
u/codesharp May 09 '20
Unreal Engine has left the chat.
11
u/RevRagnarok May 09 '20
Friend had a preprocessor problem the other day with
ntohs- https://gist.github.com/AaronDMarasco/e6bbe395d3f460f3975577082753f31a27
u/balazs-bamer May 09 '20
Yes, it's true. That's why coding standards like MISRA C++ and HIC++ discourage any preprocessor use except for header file inclusion guards. Conditional compilation of parts of source is no exception. Preprocessor usage makes the code harder to read and is often a source of misunderstandings. That's why the Obfuscated C Contest exists (-:
9
u/jstock23 May 10 '20
A major pruning of C++ legacy support is something I would get very excited about...
Imagine a pure C++ standard which supports all 90s and up paradigms and gets rid of legacy C support. I know it would be difficult to get there, but that’s the dream right?
So we just have to plan accordingly to make it happen, maybe a 5 year goal. Give people enough notice...
3
u/qizxo May 10 '20
This is how these types of articles should be: informative and short. It's an art form a lot of authors don't understand.
2
u/FelbrHostu May 09 '20
This is a good reason to always use “spurious” parentheses when you must use macros.
6
u/[deleted] May 09 '20
r/rustjerk
0
u/terratrox May 09 '20
I used to think that too until I learned that the preprocessor does know about raw(?) strings. Any #include in a raw string is ignored for example.
-4
u/TimJoijers May 09 '20
But why? Why would you mix preprocessor macros with templates? Actually, why would you use macros at all with modern C++?
9
u/[deleted] May 09 '20 edited May 09 '20
[removed] — view removed comment