{"id":1342,"date":"2022-08-30T15:15:44","date_gmt":"2022-08-30T15:15:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/are-all-macros-evil-duplicate-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:44","modified_gmt":"2022-08-30T15:15:44","slug":"are-all-macros-evil-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/are-all-macros-evil-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"Are all macros evil? [duplicate]-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ll ignore macros that don&#8217;t take parameters &#8211; as litb says, if you want to tell the preprocessor what code to process, such as in include guards, they&#8217;re the business. For many other purposes, a constant does as well.<\/p>\n<p>If you have an idea for a parameterized macro, but realise you can replace it with a function template, then do so. The same goes if you can replace it with a struct template, perhaps using a function as well.<\/p>\n<p>If you can&#8217;t replace it with templates (or, to be precise, if you can&#8217;t figure out how to do so), then assess whether to use a macro, or just suck it up and write things out longhand each time.<\/p>\n<p>The thing about macros is that you&#8217;re mixing two languages (the preprocessor language with &#8220;proper&#8221; C++). This creates headaches, because the compiler only understands one of them at a time, and so will cheerfully allow function-style macros which behave totally unlike actual function calls, which they syntactically resemble at the point of use. But there are cases where it&#8217;s worth it.<\/p>\n<p>One example is the &#8220;verbose logging macro&#8221;: it does something you can&#8217;t do without the preprocessor or messy duplication:<\/p>\n<pre><code>#define LOG(x) (std::cout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:46:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll ignore macros that don&#8217;t take parameters &#8211; as litb says, if you want to tell the preprocessor what code to process, such as in include guards, they&#8217;re the business. For many other purposes, a constant does as well. If you have an idea for a parameterized macro, but realise you can replace it with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1342","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1342"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1342\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}