Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

21
  • 25
    You're right that the consumer of a const reference can't expect that the referent will not be modified elsewhere. const (as a qualifier on pointers and references) is all about sharing a read-only view of an object, not making objects immutable. But I disagree that it's useless. If a function accepts a const reference (or pointer-to-const), it had better not try to modify the referenced object, except as allowed by the mutable modifier, because I might have passed in a truly constant object stored in read-only memory. Commented Nov 11, 2010 at 0:49
  • 19
    (continued) C++ const_cast (and equivalent mechanisms for stripping const) are forbidden unless the function can guarantee that the object was not const at the point of definition, which effectively limits their use to private internal helper functions. For a public API in C++ to try to modify an object passed in via const reference (or pointer) violates the standard and invokes undefined behavior. Commented Nov 11, 2010 at 0:52
  • 9
    @Ben: I did not say that const in C++ is useless. It is very useful. I use it all the time. I said that it was unreliable, misleading and dangerous. There are plenty of things that are unreliable, misleading and dangerous that are still of use. That's not the question at hand. The question at hand is "should the C# language replicate this unreliable, misleading and dangerous feature?" and the answer is "no" because the benefit of implementing a questionable design does not justify the large cost of doing so. Commented Nov 11, 2010 at 15:58
  • 18
    Const is not broken in C/C++ and it is not misleading either. It guarantees exactly as much as any other type information. C and C++ have weak typing. Type information never gives any guarantee there. It is nevertheless excellent tool for catching mistakes. Commented Mar 6, 2014 at 10:45
  • 12
    You're talking "suck and blow" semantics when it comes to feature justification. If a feature is deemed useful, it's perfectly valid to question why it wasn't included. A more appropriate summation would be to say the feature's implementation cost outweighed its benefits, rather than dismissing the question as invalid as a matter of principle. Commented May 23, 2014 at 19:20