{"id":1315,"date":"2014-02-12T20:38:00","date_gmt":"2014-02-12T19:38:00","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=1315"},"modified":"2014-02-12T20:38:00","modified_gmt":"2014-02-12T19:38:00","slug":"c11-contextually-converted-to-bool","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2014\/02\/12\/c11-contextually-converted-to-bool\/","title":{"rendered":"C++11 &#8211; Contextually Converted to bool"},"content":{"rendered":"<p>In C++, a one-argument constructor can implicitly be used to convert some source value or object into an instance of your class.<br \/>\nTo prevent this you can mark your one-argument constructors as explicit. In that case, if you want the conversion you have to explicitly perform the casting.<br \/>\nC++11 adds this functionality to conversion operators as well, however, its behavior surprised me.<br \/>\nFor example:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;iostream&gt;\r\n\r\nusing namespace std;\r\n\r\nclass Foo\r\n{\r\npublic:\r\n\texplicit operator bool() const { return true; }\r\n};\r\n\r\nint main()\r\n{\r\n\tFoo f;\r\n\tif (f)\r\n\t{\r\n\t\tcout &lt;&lt; &quot;f is true&quot; &lt;&lt; endl;\r\n\t}\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>The output of this piece of code is &#8220;f is true&#8221;.<br \/>\nIn other words, the compiler is implicitly converting f into a Boolean, even though the conversion operator is marked as explicit.<br \/>\nThis surprised me. I immediately blamed it on a compiler bug. However, after some more investigation, it turns out the C++ standard has this notion of <strong>&#8220;contextually converted to bool&#8221;<\/strong>.<br \/>\nThe C++ standard has a paragraph explaining this behavior, section 4 Standard conversions [conv]<\/p>\n<blockquote><p>3 An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t (8.5).<br \/>\n4 Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5).<\/p><\/blockquote>\n<p>Searching through the standard, an expression can be contextually converted to bool in several cases, not only in the above if-statement.<br \/>\nI also found <a href=\"http:\/\/chris-sharpe.blogspot.be\/2013\/07\/contextually-converted-to-bool.html\" target=\"_blank\">this blog post explaining this behavior<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++, a one-argument constructor can implicitly be used to convert some source value or object into an instance of your class. To prevent this you can mark your one-argument constructors as explicit. In that case, if you want the conversion you have to explicitly perform the casting. C++11 adds this functionality to conversion operators [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":9,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1315\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}