From: Seungbeom Kim on
On 2010-03-30 08:48, Peter C. Chapin wrote:
> Seungbeom Kim wrote:
>
>> On the other hand, I seldom (almost never) see things like:
>>
>> ssize_t read(const int fd, void *const buf, const size_t count);
>>
>> instead of
>>
>> ssize_t read(int fd, void *buf, size_t count);
>
> I'm not suggesting adding const to the parameters in the declaration of read.
> Its existence in the definition is a local implementation decision.

Yes, I was talking about the definition in the implementation file.
(I added confusion by using ';' instead of '{ ... }'.)

And it is not the specific function (read) but the style that I was
talking about. I don't remember seeing such a style in The C Programming
Language, The C++ Programming Language, or GNU coreutils, etc.
For example, I haven't seen anything like below:

int main(const int argc, char** const argv) { ... }

void sighandler(const int signum) { ... }

reference operator[](const size_type n) { /* return p[n]; */ }

>> In addition, adding const to buf and count would disallow the style of
>> implementation that increments buf and decrements count internally,
>> which is a perfectly valid way.
>
> In that case, don't add the const. Thus in the header:
>
> ssize_t read(int fd, void *buf, size_t count);
>
> In the implementation file
>
> ssize_t read(const int fd, void *buf, size_t count)
> {
> // Here buf and count can be modified.
> // However, fd can not be accidentally modified.
> }

Of course. I was saying that I was a bit hesitant to add or remove
const depending on the implementation detail. Even though it does not
affect the interface. Perhaps it's because, though const in the
parameters for a function definition belongs to the realm of
implementation detail, it doesn't look so or feel so; it is not buried
in the function body but stands out too much in the declaration line.

In addition, the simpler the function is, the more cluttered it makes
the function to add const, and the less useful. In simplest functions
where it's so obvious that the parameters are not modified, people
don't seem to bother to add const. (And few textbooks teach programmers
with examples like 'int add(const int x, const int y) { return x+y; }'.)

Then comes the matter of consistency.

This is what I think why I haven't done it, which is not why you,
anyone else, or even I shouldn't do it, but it may be enlightening
in some ways...

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]