{"id":406,"date":"2009-03-24T17:58:58","date_gmt":"2009-03-24T17:58:58","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=406"},"modified":"2009-03-25T18:48:53","modified_gmt":"2009-03-25T18:48:53","slug":"tomalaks-tuesday-tip-3-when-a-macro-is-not-a-macro","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2009\/03\/tomalaks-tuesday-tip-3-when-a-macro-is-not-a-macro\/","title":{"rendered":"Tomalak&#039;s Tuesday Tip #3: When A Macro Is Not A Macro"},"content":{"rendered":"<p>A question came up on IRC today regarding preprocessor macros and which ones are standard. Amazingly, <a title=\"Predefined Macros (C\/C++)\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/b0084kay(VS.80).aspx\">this MSDN article<\/a> does a good job of describing the standard macros and lists those non-standard but conventional macros implemented in Visual Studio.<\/p>\n<p>What it doesn&#039;t do is mention that some of those macros in fact aren&#039;t macros at all, but implicitly defined variables. It goes so far as to mention that compiling with <code>\/E<\/code> or <code>\/EP<\/code> parameters (equivalent to <code>g++ -E<\/code> or <code>| cpp<\/code>, running the preprocessor over source code and doing nothing else) will not expand the &#039;macros&#039;, but it is still very misleading as to why.<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">int main() {\n<strong>FILE<\/strong>\n<strong>LINE<\/strong>\n<strong>FUNCTION<\/strong>\n}<\/textarea>\n<\/p>\n<p>This is not valid code so compilation would fail, but after running the preprocessor you get the following output:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">$ g++ -E test.cpp\nint main() {\n\"test.cpp\"\n4\n<strong>FUNCTION<\/strong>\n}<\/textarea>\n<\/p>\n<p>This demonstrates that <code>__FUNCTION__<\/code> isn&#039;t actually a preprocessor macro, but an implicitly defined variable (in fact, a <code>const char*<\/code> though this example doesn&#039;t show it). <code>__FILE__<\/code> and <code>__LINE__<\/code> are macros and as such are evaluated by the preprocessor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A question came up on IRC today regarding preprocessor macros and which ones are standard. Amazingly, MSDN does a good job of describing the standard macros and lists those non-standard but conventional macros implemented in Visual Studio. What it doesn&#039;t do is mention that some of those macros in fact aren&#039;t macros at all, but implicitly defined variables.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,20,12,33],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/406"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":2,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/406\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}