{"id":3588,"date":"2018-07-15T09:29:28","date_gmt":"2018-07-15T08:29:28","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=3588"},"modified":"2018-07-16T20:11:56","modified_gmt":"2018-07-16T19:11:56","slug":"c-guns-why-c-dont-need-python-pep-572-assignment-expressions","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=3588","title":{"rendered":"C++ Guns: Why C++ don't need Python PEP 572 Assignment Expressions"},"content":{"rendered":"<p>It is often a failure to assign a variable inside a if() instead to compare it. One can easily mistype '==' to '='. Both times the result can be converter to bool. To prevent this kind of error one languages simply forbid assignments inside if(), others have or introduce new operators.<\/p>\n<p>But this is all unnecessary. Just make the variable 'const' so it's value can not change after the first assignment. No need for new operators or heavy discussions. But as far as I know there is no keyword 'const' to make the value immutable in python and the new language julia.<br \/>\nIn C++ it is always possible to make a variable 'const'. The compile decides to reuse the memory the variable occupied. You don't have to do this by your own.<\/p>\n<p>Example:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nconst auto x = func();\r\nif(x = 5) {}\r\n<\/pre>\n<blockquote><p>error: assignment of read-only variable 'x'\n<\/p><\/blockquote>\n<h1>Advanced: Memory reuse for POD types<\/h1>\n<p>This is a simple,silly example how the compiler decide to reuse memory of one variable when it is no longer needed. Consider this code:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nextern int func();\r\nextern int func2();\r\nauto example() {\r\n    const auto x = func();\r\n    int result =  x;    \r\n\r\n    const auto y = func2(); \/\/ Memory of x is reused by y\r\n    result = result + y;\r\n\r\n    return result;\r\n}\r\n<\/pre>\n<p>The two function 'func' and 'func2' are marked as 'extern' so they can't optimized away. The memory of variable x is reused by variable y, because x is no longer needed. The compiler can decide this without any hint from the user. There is no need to explicit define a scope or lifetime of variable x.<br \/>\nLet's introspect the generated assembler code:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexample():\r\n        call    func()      # call func store return value in x\r\n        movl    %eax, %ebx  # copy x to result\r\n        call    func2()     # call func2 store return value in y\r\n        addl    %ebx, %eax  # add result to y\r\n        ret                 # return y \r\n<\/pre>\n<p>The result of a call to 'func' in line 1 is stored in register eax. This is the variable x. And the result of a call to 'func2' in line 3 is stored in register eax too. This is variable y now.  The variable 'result' is stored in register ebx. No more memory is in use. <\/p>\n<p>We have three variables but only two register in use. This is because I wrote a pretty silly example. You may also notice that in the C++ code the variable 'result' is returned. But in assembler the variable 'y' is returned. But the semantic is the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is often a failure to assign a variable inside a if() instead to compare it. One can easily mistype '==' to '='. Both times the result can be converter to bool. To prevent this kind of error one languages simply forbid assignments inside if(), others have or introduce new operators. But this is all [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-3588","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3588"}],"version-history":[{"count":7,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3588\/revisions"}],"predecessor-version":[{"id":3595,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3588\/revisions\/3595"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3588"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}