{"id":10950,"date":"2015-04-03T13:19:53","date_gmt":"2015-04-03T17:19:53","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10950"},"modified":"2015-04-03T13:19:53","modified_gmt":"2015-04-03T17:19:53","slug":"preprocessor-abuse-and-optional-parentheses","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/04\/03\/preprocessor-abuse-and-optional-parentheses\/","title":{"rendered":"Preprocessor Abuse and Optional Parentheses"},"content":{"rendered":"<p><a href=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-03-20-preprocessor-abuse-and-optional-parentheses.html\">Mike Ash<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-03-20-preprocessor-abuse-and-optional-parentheses.html\">\n<p>While coming up with macros that would justify this [<code>UNPAREN()<\/code>] construct, I built a nice <code>dispatch_once<\/code> macro for making lazily-initialized constants. Here it is:<\/p>\n<pre>\n#define ONCE(type, name, ...) \\\n    UNPAREN(type) name() { \\\n        static UNPAREN(type) static_ ## name; \\\n        static dispatch_once_t predicate; \\\n        dispatch_once(&amp;predicate, ^{ \\\n            static_ ## name = ({ __VA_ARGS__; }); \\\n        }); \\\n        return static_ ## name; \\\n    }\n<\/pre>\n<p>Here&rsquo;s an example use:<\/p>\n<pre>ONCE(NSSet *, AllowedFileTypes, [NSSet setWithArray: @[ @\"mp3\", @\"m4a\", @\"aiff\" ]])<\/pre>\n<p>Then you can call AllowedFileTypes() to obtain the set, and it's efficiently created on demand. In the unlikely event that the type contains a comma, you can add parentheses and it will still work.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Mike Ash: While coming up with macros that would justify this [UNPAREN()] construct, I built a nice dispatch_once macro for making lazily-initialized constants. Here it is: #define ONCE(type, name, ...) \\ UNPAREN(type) name() { \\ static UNPAREN(type) static_ ## name; \\ static dispatch_once_t predicate; \\ dispatch_once(&amp;predicate, ^{ \\ static_ ## name = ({ __VA_ARGS__; }); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,30,941,54,71],"class_list":["post-10950","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-mac","tag-macros","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=10950"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10950\/revisions"}],"predecessor-version":[{"id":10951,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10950\/revisions\/10951"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}