{"id":1501,"date":"2016-01-16T03:21:57","date_gmt":"2016-01-15T17:21:57","guid":{"rendered":"http:\/\/brnz.org\/hbr\/?p=1501"},"modified":"2016-01-16T03:21:57","modified_gmt":"2016-01-15T17:21:57","slug":"another-another-c11-countof","status":"publish","type":"post","link":"https:\/\/brnz.org\/hbr\/?p=1501","title":{"rendered":"Another another C++11 &#8216;countof&#8217;"},"content":{"rendered":"<p>My <a href=\"https:\/\/brnz.org\/hbr\/?p=1491\">earlier\u00a0post<\/a> received\u00a0<a href=\"https:\/\/www.reddit.com\/r\/cpp\/comments\/40znpn\/another_c11_countof\/cyyn7hr\">this comment<\/a>\u00a0which is a pretty neat little improvement over <a href=\"http:\/\/www.g-truc.net\/post-0708.html\">the one from g-truc.net<\/a>.<\/p>\n<p>Here it is, with one further tweak:<\/p>\n<pre escaped=\"true\" lang=\"cpp\" line=\"1\">template&lt;typename T, std::size_t N&gt;\r\nconstexpr \r\nstd::integral_constant&lt;std::size_t, N&gt; countof(T const (&amp;)[N]) noexcept\r\n{\r\n\u00a0 return {};\r\n}\r\n\r\n#define COUNTOF(...) decltype(countof(__VA_ARGS__))::value<\/pre>\n<p>The change I&#8217;ve made to pfultz2&#8217;s version is to use <code>::value<\/code> rather than <code>{}<\/code> after\u00a0<code>decltype<\/code> in the macro.<\/p>\n<p>This makes the type of the result <code>std::size_t<\/code>\u00a0not <code>std::integral_constant<\/code>, so\u00a0it can be used in va_arg settings without triggering compiler or static analysis warnings.<\/p>\n<p>It also has the advantage of not triggering extra warnings in VS2015U1 (<a href=\"https:\/\/connect.microsoft.com\/VisualStudio\/feedback\/details\/2242614\">this issue<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My earlier\u00a0post received\u00a0this comment\u00a0which is a pretty neat little improvement over the one from g-truc.net. Here it is, with one further tweak: template&lt;typename T, std::size_t N&gt; constexpr std::integral_constant&lt;std::size_t, N&gt; countof(T const (&amp;)[N]) noexcept { \u00a0 return {}; } #define COUNTOF(&#8230;) decltype(countof(__VA_ARGS__))::value The change I&#8217;ve made to pfultz2&#8217;s version is to use ::value rather than {} &hellip; <a href=\"https:\/\/brnz.org\/hbr\/?p=1501\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Another another C++11 &#8216;countof&#8217;&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,26],"tags":[],"_links":{"self":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1501"}],"collection":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1501"}],"version-history":[{"count":2,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1501\/revisions"}],"predecessor-version":[{"id":1503,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=\/wp\/v2\/posts\/1501\/revisions\/1503"}],"wp:attachment":[{"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brnz.org\/hbr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}