HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Why is there readonly (const) in C++, but not writeonly (sink)?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
whywriteonlyreadonlybutconstsinktherenot

Problem

C++ initially had "readonly" and "writeonly" qualifiers, before "readonly" was renamed to "const", and "writeonly" removed. My question is: What are the problems with "writeonly" (or a "sink" qualifier)?

There are (non-object oriented) programming languages with "in", "out" and "inout" qualifier for functions arguments. In that context, the meaning of "out" is pretty clear. Writing "result_p = result_p+1" is not allowed if "out int& result_p", and the question whether "result_p += 1" is allowed doesn't arise, because these languages don't have "+=" and similar constructs.

Now "sink" in C++ would be a type qualifier, not just a function argument qualifier. The meaning of "sink" is clear on a semantic level. Information is only allowed to flow into a "sink" qualified object, but no information is allowed to leave a "sink" qualified object. So writing "result_p += 1" would be allowed if "sink int& result_p", but what about "result_p = result_p+1"? Its semantic is fine, but there seem to be no general way to check this based on simple syntax or typing rules. But so what, does anything bad happens if "result_p = result_p+1" is not allowed?

Solution

I can only hypothesize, so maybe this should only be a comment, but it's too long for the comment box.

In general Stroustrup tries extremely hard to avoid adding new keywords to the language, so if he discovered that there was another way to get whatever "expressivity" writeonly might provide he would remove the keyword.

My guess is that he actually couldn't find much use for writeonly. Stroustrup came from an Operating Systems background, and (according to [1]) a lot of the stuff about const and friend originally derived from analogies to O.S. access rights. (For const, think read/write/execute permissions on files in Unix, and for friend think of one program granting access permissions to another program.)

The analogy apparently only goes so far though. The usefulness of a writeonly parameter would seem to be that it allows you to return more than one value (but you can return a pair or a struct in C++, and you can "return" an error code by throwing an exception, and perhaps one return value plus an error code is the common case.) Declaring a non-parameter variable as writeonly makes no sense, because then you would need to do a cast in order to do anything with the value. A writeonly function is a constructor, and C++ already has those using a different syntax (that doesn't require a new keyword.) An append operation for vectors most certainly can not be writeonly, because it must increment the length of the vector (i.e., length = length+1.)

[1] Bjarne Stroustrup, The Design and Evolution of C++, Addison-Wesley, 1994.

Context

StackExchange Computer Science Q#28002, answer score: 4

Revisions (0)

No revisions yet.