{"id":1078,"date":"2013-07-01T01:00:00","date_gmt":"2013-06-30T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1078"},"modified":"2013-08-12T11:41:17","modified_gmt":"2013-08-12T10:41:17","slug":"not-everything-is-a-reference","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1078","title":{"rendered":"Not Everything Is A Reference"},"content":{"rendered":"<p>I like Java as a language. Kind of. It\u00e2\u20ac\u2122s strongly and statically typed, has object oriented syntax, reflection, exceptions, and interfaces (interfaces being one of the few features that I think C++ is missing). Dynamically typed languages encourage sloppiness, and don\u00e2\u20ac\u2122t help you avoid sloppiness, and the overriding feature of all good software is that it is readable (i.e.\u00c2\u00a0not sloppy). However, like many languages, I feel that it\u00e2\u20ac\u2122s been designed because \u00e2\u20ac\u0153C++ is too hard\u00e2\u20ac\u009d. Further, like many languages that do this, they end up in difficulties that are created because fundamentally you can\u00e2\u20ac\u2122t hide complexity. As Einstein said, \u00e2\u20ac\u0153Make things as simple as possible; but not simpler.\u00e2\u20ac\u009d<\/p>\n<p>This article is a discussion of one of those difficulties.<\/p>\n<p>Consider this Java code:<\/p>\n<pre class=\"sourceCode java\"><code class=\"sourceCode java\">    <span class=\"kw\">class<\/span> SomeClass <span class=\"kw\">extends<\/span> SomeOtherClass {\n        <span class=\"kw\">private<\/span> <span class=\"dt\">final<\/span> String NAME = <span class=\"st\">&quot;SomeClass&quot;<\/span>;\n\n        <span class=\"fu\">@Override<\/span>\n        <span class=\"kw\">public<\/span> String <span class=\"fu\">getName<\/span>() {\n            <span class=\"kw\">return<\/span> NAME;\n        }\n    }<\/code><\/pre>\n<p>Here\u00e2\u20ac\u2122s the same in C++11:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    class SomeClass : public SomeOtherClass {\n        public:\n            <span class=\"dt\">const<\/span> string &amp;getName() <span class=\"dt\">const<\/span> override {\n                <span class=\"kw\">return<\/span> NAME;\n            }\n        private:\n            <span class=\"dt\">static<\/span> <span class=\"dt\">const<\/span> string NAME;\n    };\n    <span class=\"dt\">const<\/span> string SomeClass::NAME = <span class=\"st\">&quot;SomeClass&quot;<\/span>;<\/code><\/pre>\n<p>These, while superficially similar, are very different. In C++ we have a way of defining constants. We have a way of returning references to constants, and a way of saying a particular method doesn\u00e2\u20ac\u2122t alter the object. In Java we have none of those.<\/p>\n<p>This lack of expressive ability is, presumably, to keep things simple. Unfortunately, wanting to keep things simple, doesn\u00e2\u20ac\u2122t make them so. Imagine this code:<\/p>\n<pre class=\"sourceCode java\"><code class=\"sourceCode java\">    <span class=\"kw\">public<\/span> <span class=\"kw\">class<\/span> references {\n        <span class=\"kw\">abstract<\/span> <span class=\"dt\">static<\/span> <span class=\"kw\">class<\/span> SomeOtherClass {\n            <span class=\"kw\">abstract<\/span> <span class=\"kw\">public<\/span> String <span class=\"fu\">getName<\/span>();\n        }\n    \n        <span class=\"dt\">static<\/span> <span class=\"kw\">class<\/span> SomeClass <span class=\"kw\">extends<\/span> SomeOtherClass {\n            <span class=\"kw\">private<\/span> <span class=\"dt\">final<\/span> String NAME = <span class=\"st\">&quot;SomeClass&quot;<\/span>;\n    \n            <span class=\"fu\">@Override<\/span>\n            <span class=\"kw\">public<\/span> String <span class=\"fu\">getName<\/span>() {\n                <span class=\"kw\">return<\/span> NAME;\n            }\n        }\n    \n        <span class=\"kw\">public<\/span> <span class=\"dt\">static<\/span> <span class=\"dt\">void<\/span> <span class=\"fu\">main<\/span>(String[] args) {\n            SomeClass x;\n            String someString = x.<span class=\"fu\">getName<\/span>();\n    \n            System.<span class=\"fu\">out<\/span>.<span class=\"fu\">println<\/span>(x.<span class=\"fu\">getName<\/span>());\n            someString[<span class=\"dv\">0<\/span>] = &#39;s&#39;;\n            System.<span class=\"fu\">out<\/span>.<span class=\"fu\">println<\/span>(x.<span class=\"fu\">getName<\/span>());\n        }\n    }<\/code><\/pre>\n<p>This is invalid. Java <code>String<\/code>s are immutable. so the attempt to alter the first character isn\u00e2\u20ac\u2122t supported. Instead we have to reach for <code>StringBuilder<\/code> or <code>String.replace()<\/code> to create a new string from an existing string. In this case, that\u00e2\u20ac\u2122s exactly what\u00e2\u20ac\u2122s necessary \u00e2\u20ac\u201c we can\u00e2\u20ac\u2122t be allowed to modify constants.<\/p>\n<p>Except, what if we want to modify a non-const string? Since all <code>String<\/code>s are immutable, we have to copy <em>every<\/em> string before we modify it. Worse, the immutability of a <code>String<\/code> only comes about because <code>String<\/code> has been carefully coded so that it is immutable. We will have the same problem with any container type we might create of our own. We\u00e2\u20ac\u2122ll have to make it immutable, and then remember to supply all the methods that might have modified in place with versions that self-copy then modify the copy.<\/p>\n<p>All to avoid \u00e2\u20ac\u0153const\u00e2\u20ac\u009d, and all because Java only understands references to objects.<\/p>\n<p>C++11\u00e2\u20ac\u2122s new move semantics, strangely, let C++ go in the opposite direction \u00e2\u20ac\u201c avoiding references. I say \u00e2\u20ac\u0153strangely\u00e2\u20ac\u009d because at first sight, and for the experienced C++ programmer, references are what let you write fast software. Everyone knows you shouldn\u00e2\u20ac\u2122t, where possible, copy data. You should pass around references. We end up passing pointers or references (pointers in disguise) around. The problem is that that had its own pitfalls:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">   SomeClass &amp;manipulateSomeClasses(<span class=\"dt\">const<\/span> SomeClass &amp;A, <span class=\"dt\">const<\/span> SomeClass &amp;B)\n   {\n       SomeClass *result = new SomeClass();\n       <span class=\"co\">\/\/ ... Combine A and B into result ...<\/span>\n       <span class=\"kw\">return<\/span> *result;\n   }<\/code><\/pre>\n<p>Who is responsible for freeing up the memory allocated here? We\u00e2\u20ac\u2122re returning a reference, so unless the caller immediately takes the address of this return value, it\u00e2\u20ac\u2122s not going to happen. Further, unless they assign the answer as part of a reference construction, there is no reasonable way to get at the returned reference:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    SomeClass &amp;reference( manipulateSomeClasses(A,B) );\n    <span class=\"co\">\/\/ ... work on reference ...<\/span>\n    delete &amp;reference;<\/code><\/pre>\n<p>Yuck, deleting with the address-of operator. Notice as well that the caller of <code>manipulateSomeClasses()<\/code> has to know implementation detail \u00e2\u20ac\u201c that its storage is allocated on the free store rather than automatic storage. Most of us have simply given in at this point and either passed a destination object in as a parameter, or returned a pointer and taken over ownership of it. Neither of which is pretty (passing a destination parameter is classic C, but makes the code less self documenting), and still requires far too much knowledge of internals.<\/p>\n<p>So what do we do instead? Return by value?<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">   SomeClass manipulateSomeClasses(<span class=\"dt\">const<\/span> SomeClass &amp;A, <span class=\"dt\">const<\/span> SomeClass &amp;B)\n   {\n       SomeClass result;\n       <span class=\"co\">\/\/ ... Combine A and B into result ...<\/span>\n       <span class=\"kw\">return<\/span> result;\n   }<\/code><\/pre>\n<p>Assuming <code>SomeClass<\/code> has all the requisite copy constructors and assignment operators, this will at least protect us from a memory leak. If <code>SomeClass<\/code> holds a lot of data though, returning results like this is slow. The returned value has to be copy-constructed into its target in the caller from the automatic <code>result<\/code> that\u00e2\u20ac\u2122s about to be destructed.<\/p>\n<p>Or at least it was in C++98. C++11 lets you create a move constructor. We\u00e2\u20ac\u2122ve seen previously how useful that is for construction, but it might not be entirely clear that a move constructor can be called implicitly in these return-by-value situations. The above return-by-value example, provided a C++11 move-constructor has been implemented, is not slow. In fact it\u00e2\u20ac\u2122s wonderfully fast.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    SomeClass x( manipulateSomeClasses(A,B) );<\/code><\/pre>\n<p><code>x<\/code> here in the old days would be copy-constructed. Now, the compiler notes that the return value of <code>manipulateSomeClasses()<\/code> is a temporary, and so the move-constructor can be used. The move constructor effectively reassigns the storage of that temporary to the non-temporary, <code>x<\/code> \u00e2\u20ac\u201c nothing is copied.<\/p>\n<p>We now have no worries about speed, and no worries about remembering to free up resources.<\/p>\n<p>Let\u00e2\u20ac\u2122s return to where we started then.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    <span class=\"ot\">#include &lt;iostream&gt;<\/span>\n    <span class=\"ot\">#include &lt;string&gt;<\/span>\n    using namespace std;\n\n    class SomeOtherClass {\n      public:\n        virtual <span class=\"dt\">const<\/span> string &amp;getName() <span class=\"dt\">const<\/span> = <span class=\"dv\">0<\/span>;\n    };\n\n    class SomeClass : public SomeOtherClass {\n        public:\n            <span class=\"dt\">const<\/span> string &amp;getName() <span class=\"dt\">const<\/span> override {\n                <span class=\"kw\">return<\/span> NAME;\n            }\n        private:\n            <span class=\"dt\">static<\/span> constexpr string NAME {<span class=\"st\">&quot;SomeClass&quot;<\/span>};\n    };\n    <span class=\"dt\">const<\/span> string SomeClass::NAME = <span class=\"st\">&quot;SomeClass&quot;<\/span>;\n\n    <span class=\"dt\">int<\/span> main()\n    {\n        SomeClass x;\n        string name(x.getName());\n        cerr &lt;&lt; <span class=\"st\">&quot;name = &quot;<\/span> &lt;&lt; name &lt;&lt; <span class=\"st\">&quot;  &quot;<\/span> &lt;&lt; x.getName() &lt;&lt; endl;\n        name[<span class=\"dv\">0<\/span>] = &#39;s&#39;;\n        cerr &lt;&lt; <span class=\"st\">&quot;name = &quot;<\/span> &lt;&lt; name &lt;&lt; <span class=\"st\">&quot;  &quot;<\/span> &lt;&lt; x.getName() &lt;&lt; endl;\n\n        <span class=\"co\">\/\/ outputs:<\/span>\n        <span class=\"co\">\/\/ name = SomeClass<\/span>\n        <span class=\"co\">\/\/ name = someClass<\/span>\n\n        <span class=\"kw\">return<\/span> <span class=\"dv\">0<\/span>;\n    }<\/code><\/pre>\n<p>This one will work exactly as you might expect, with no need for builder classes or unnecessary copies (in fact the copy here <em>is<\/em> necessary, but the compiler will know that and do the right thing). In other words: our code is more readable than the Java equivalent, and will be faster when it\u00e2\u20ac\u2122s possible to be faster. Importantly, the user of <code>SomeClass<\/code> doesn\u00e2\u20ac\u2122t need to care about its internals, it could be complex or simple, use lots of storage or none. The caller treats it as they would any built-in type.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like Java as a language. Kind of. It\u00e2\u20ac\u2122s strongly and statically typed, has object oriented syntax, reflection, exceptions, and interfaces (interfaces being one of the few features that I think C++ is missing). Dynamically typed languages encourage sloppiness, and don\u00e2\u20ac\u2122t help you avoid sloppiness, and the overriding feature of all good software is that\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1078\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[66,82,79,83,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1078"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1078"}],"version-history":[{"count":4,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1078\/revisions"}],"predecessor-version":[{"id":1198,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1078\/revisions\/1198"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}