Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why to replace an existing keyword in C/C++ with a macro?

I often come across keywords or one-word identifiers defined with another name. For example, boost defines noexcept as BOOST_NOEXCEPT, some C++ standard libraries replace [[nodiscard]] with _NODISCARD, Windows API is prone to introduce their own macros as well, and so forth. I didn’t really manage to find anything which would explain this.

This makes an unfamiliar person search for what such macros mean which therefore makes the code a bit harder to understand (at least this is how I see it). Why is such tendency so widespread? What is the purpose of replacing existing one-word constructs with macros?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The most useful case of these is when you target both compilers that support a new feature and ones that do not.
For instance:

#if CompilerSupportsNoexcept
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading