PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] Why do libraries create macro aliases for c++ keywords?

 July 09, 2022     c++, keyword, macros     No comments   

Issue

Often in large libraries I see code similar to the following:

#define MY_CONSTEXPR constexpr
#define MY_NOEXCEPT noexcept
#define MY_NODISCARD [[nodiscard]]
etc.

What is the purpose/benefit to creating aliases for these keywords? I see this fairly commonly but couldn't find anything regarding the practice. If I had to guess, the reason is so you can conditionally compile whether you want these keywords present in your compilation.


Solution

What is the purpose/benefit to creating aliases for these keywords? I see this fairly commonly but couldn't find anything regarding the practice. If I had to guess, the reason is so you can conditionally compile whether you want these keywords present in your compilation.

Yes, generally so. You may also see similar definitions with empty replacement text, such as

#define MY_CONST

, which cover the case where you do not want the keyword to be used.

The bigger picture is that this facilitates compatibility with various versions of the language and with various compilers. Such definitions may be written when the library is built (generally as appropriate for the toolchain being used to build it), or they may be wrapped in conditional-compilation directives that attempt to decide how to define the macros at the time that client programs are compiled.



Answered By - John Bollinger
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing