Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why doesn't C++ seem to support creating classes whose objects can be only const? In other words, classes where you get a compile-time error if you try to create a non-const object of it. This seems like a natural thing to do in many cases, yet C++ doesn't allow it. What is the reason behind this?</p> </blockquote> <p>The first question that comes to mind is when having a type for which all objects are <code>const</code> is a natural thing. There are types that are naturally <em>immutable</em>, in some languages it's even recommended to make as many types as possible immutable, but that is not the same thing as enforcing that all objects are <code>const</code>.</p> <p>In your class design it is trivial to control whether any of your member functions modifies the state of the object achieving an immutable object. Encapsulation will then do its magic, and if you don't expose any mutating function, then the type is <em>immutable</em> and as good as <code>const</code>. You can also (if you want the extra safety) qualify all your member <strong>data</strong> as <code>const</code> to let the compiler tell you if you violate your own constraint in your own code. Note that forcing all the objects to be <code>const</code> implicitly would mark all your member data to be <code>const</code>, and your non-const functions <strong>useless</strong>.</p> <p>Given that the high level effect can be achieved in different ways with little cost, why would you make a complex language even more complicated for no added value?</p> <p>While the question above may seem rhetoric it is not. One of the criteria to consider extensions to the language is the <em>return on investment</em>: What will we be able to do with this feature that was not possible without it? (i.e. is this an enabling feature?) If the answer is nothing, then the next question is How much simpler will programmer's life be with this feature compared to the current state? (for non-enabling features, what is the value of adding them?)</p> <p>In the <em>enabling</em> category the example would be <em>rvalue-references</em>; without that language feature, you cannot implement <em>perfect-forwarding</em>, nor <em>move-semantics</em>. In the second category you can consider lambdas &mdash; there is nothing that can be done with lambdas that was not doable without them &mdash; whose value lies on simplifying user code (code that would have to be convoluted: create a functor, declare all members of the proper types, provide a constructor that initializes &mdash; does the capture &mdash; the members&hellip;) but the code becomes much simpler with lambdas. Note that it took quite a bit of arguing from Sutter to convince Stroustrup that lambdas had value.</p> <p>In your case, what you are asking for is not an enabling feature, and the cost of supporting the use cases without the feature is low enough that it does not grant providing that as a core language feature.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload