{"id":569,"date":"2010-09-14T19:49:29","date_gmt":"2010-09-14T19:49:29","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=569"},"modified":"2011-06-22T09:26:44","modified_gmt":"2011-06-22T09:26:44","slug":"clang-breaks-template-rules-confuses-me","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2010\/09\/clang-breaks-template-rules-confuses-me\/","title":{"rendered":"Clang Breaks Template Rules, Confuses Me"},"content":{"rendered":"<p>A friend posed a question this evening as to why the following code should not be accepted as valid C++03:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">enum { a = 2 };\nint main() {\nchar c;\nstd::fill(&c, &c, a);\n}<\/textarea>\n<\/p>\n<p>This code is accepted by clang, but rejected by (at least) g++ 4.4-4.6 with the diagnostic:<\/p>\n<p><code>error: no matching function for call to `fill(unsigned char*&amp;, unsigned char*, Foo::&lt;anonymous enum&gt;)`<\/code><\/p>\n<p>At first I blamed too many implicit conversion steps but, after a short discussion, the answer turned out to be quite simple: the <code>enum<\/code> is anonymous, and <code>std::fill<\/code> is a template whose third implicit argument here is an anonymous type&#8230; which is impossible in C++03.<\/p>\n<p>If we pass the value as the <code>int<\/code> that it was always destined to become (before eventually becoming a <code>char<\/code>), we get:<\/p>\n<p><textarea name=\"code\" class=\"cpp:nocontrols:nogutter\" cols=\"60\" rows=\"10\">enum { a = 2 };\nint main() {\nchar c;\nstd::fill(&c, &c, static_cast<int>(a));\n}<\/textarea>\n<\/p>\n<p>Which behaves as one expects on all toolchains.<\/p>\n<p>C++0x will accept anonymous types as template parameters but, for now, clang appears to be in error. At least it&#039;s <a href=\"http:\/\/llvm.org\/bugs\/show_bug.cgi?id=8147\">filed<\/a>. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I briefly explore an odd case of misbehaviour from clang.<\/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],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/569"}],"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=569"}],"version-history":[{"count":5,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/569\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}