{"id":532,"date":"2020-04-18T22:39:18","date_gmt":"2020-04-18T14:39:18","guid":{"rendered":"https:\/\/markjohntaylor.com\/blog\/wordpress\/?p=532"},"modified":"2020-07-20T20:27:01","modified_gmt":"2020-07-20T12:27:01","slug":"syntax-highlighting-test","status":"publish","type":"post","link":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/2020\/04\/18\/syntax-highlighting-test\/","title":{"rendered":"Syntax Highlighting Test"},"content":{"rendered":"\n<!DOCTYPE html>\n<html>\n\n<style>\n\t.center {\n\t\twidth: 60%;\n\t\tmargin: auto;\n\t}\n\t\n\tbody{\n\t\tbackground: #CDCDB4;\n\t}\n<\/style>\n\n<head>\n\t<meta charset=\"utf-8\">\n\n   \t<!---->\n\t<link rel=\"stylesheet\" href=\"https:\/\/markjohntaylor.com\/highlight\/styles\/vs2015.css\">\n\t<script src=\"https:\/\/markjohntaylor.com\/highlight\/highlight.pack.js\"><\/script>\t\n\t<script>hljs.initHighlightingOnLoad();<\/script>\n\t\n\t\n<\/head>\n<body>\n<div class=\"center\">\nCopy constructor may not be called due to the auto-optimization of compilers. It&#8217;s quite compiler specific. See code as follows.\n<pre><code class=\"language-cpp\" style=\"font-size:16px\">#include &lt;iostream>\n\nusing namespace std;\n\nclass Point {\npublic:\n\tPoint(int a = 0, int b = 0) : x(a), y(b) {\n\t\t\/\/note that only in VS 2019, when we want print address, it won't print Ox, while Unix-like does print\n\t\tcout << \"Constructor called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\";\n\t}\n\tPoint(const Point&#038; A) : x(A.x), y(A.y) {\n\t\tcout << \"Copy constructor called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\";\n\t}\n\t\/\/ try to umcomment move constructor\/assignment\n\t\/*Point(Point&#038;&#038; A) noexcept :x{ 0 }, y{ 0 } {\n\t\tswap(x, A.x); swap(y, A.y);\n\t\tcout << \"Move constructor called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\";\n\t}\n\tPoint&#038; operator=(Point&#038;&#038; A)noexcept {\n\t\tif (this == &#038;A) return *this;\n\t\tswap(x, A.x); swap(y, A.y);\n\t\tcout << \"Move assignment called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\";\n\t\treturn *this;\n\t}*\/\n\n\tPoint&#038; operator=(const Point&#038; A) {\n\t\tx = A.x; y = A.y;\n\t\tcout << \"Copy assignment called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\";\n\t\treturn *this;\n\t}\n\n\t~Point() { cout << \"Destructor called --> P(\" << x << ',' << y << \")\" << \"\\t with address --> 0x\" << this << \"\\n\\n\"; }\n\n\tfriend Point operator+(const Point A, const Point B) {\n\t\t\/\/ try them respectively and see what's going on\n\t\t\/\/return Point(A.x + B.x, A.y + B.y);\n\t\tPoint C(A.x + B.x, A.y + B.y);\n\t\treturn C;\n\t}\n\n\tfriend ostream&#038; operator<<(ostream&#038; os, const Point&#038; A) {\n\t\tos << \"(\" << A.x << ',' << A.y << \")\";\n\t\treturn os;\n\t}\n\nprivate:\n\tint x, y;\n};\n\n\nint main()\n{\n\tPoint A(1, 2);\n\tPoint B(8, 9);\n\t\/\/ try them respectively and see what's going on\n\tPoint C;\n\tC = A + B;\n\t\/\/Point C = A + B;\n\tcout << C << \"\\n\\n\";\n\treturn 0;\n}\n\n<\/code><\/pre>\nAnd these are the results tested in VS2019.\n<figure>\n<img src=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2020\/07\/copy_ctor_called_no_optimization.png\"\n\tstyle=\"width:100%\" \/><figcaption><div style=\"text-align:center\">figure 1<\/div><\/figcaption>\n<\/figure>\n<br>\n<figure>\n<img src=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2020\/07\/copy_ctor_NOT_called_with_optimization.png\" \n\tstyle=\"width:100%\"  \/><figcaption><div style=\"text-align:center\">figure 2<\/div><\/figcaption>\n<\/figure>\n<br>\n\nTested in <a href=\"http:\/\/cpp.sh\/3f3h67\"target=\"_blank\">cpp.sh<\/a>\n<figure>\n<img src=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/wp-content\/uploads\/2020\/07\/cpp.sh_.png\" \n\tstyle=\"width:100%\"  \/><figcaption><div style=\"text-align:center\">figure 3<\/div><\/figcaption>\n<\/figure>\n\n<br>\nAlso very interesting to note that the order of copy assignment called in main function & that of destructors in which objects constructed by parameters and locals constructed in subroutine operator+, varies in differing systems.\n\n<\/div>\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>Copy constructor may not be called due to the auto-optimization of compilers. It&#8217;s quite compiler specific. See code as follows. #include &lt;iostream> using namespace std; class Point { public: Point(int a = 0, int b = 0) : x(a), y(b) { \/\/note that only in VS 2019, when we want print address, it won&#8217;t print &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/2020\/04\/18\/syntax-highlighting-test\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Syntax Highlighting Test&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"template-page-builder-no-sidebar.php","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/532"}],"collection":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":49,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":780,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/532\/revisions\/780"}],"wp:attachment":[{"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/markjohntaylor.com\/blog\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}