[Swan-dev] proposal: "const struct ip_protocol" => "ip_protocol"

Andrew Cagney andrew.cagney at gmail.com
Mon Oct 14 03:07:36 UTC 2019


On Sun, 13 Oct 2019 at 13:29, D. Hugh Redelmeier <hugh at mimosa.com> wrote:
>
> | From: Andrew Cagney <andrew.cagney at gmail.com>
>
> | because it gets confusing and is obscure; better to spell out:
> |    const something
> | where the code is being used than leave everyone wondering, for instance, given:
> |    ip_something(something *foo, const someotherthing *bar, somethingelse *baz);
> | which are typos and which are correct?  imnsho field_desc et.al.,
> | being made typedef const struct were a massive mistake
>
> Clearly I disagree (since I created most of them).
> Why is it a mistake?  Confusing and obscure?  No use should be without
> the const, so make it built-in.  The const is part of the abstraction.
> The "const" is something you should not have to think about separately
> at each use of the abstraction.

Two are separate, lets keep them that way:

- how the object is constructed
Forcing const on the type takes away any flexibility - this week the
structure may have a static initializer, next week who can say

- how the object is used
Functions spell out that a parameter is being "read only" by adding const

modern languages try to use things like "final" to differentiate
between these.  We don't have that, we have "const" so instead we get
to put "const" on parameters

> If I remember correctly, the C const qualifier is now idempotent so
> adding a redundant one doesn't generate a compiler error.

Except you'll insist on removing them:

commit 6eca8ba46cd7954c7113db7b789d3002f30f8963
Author: D. Hugh Redelmeier <hugh at mimosa.com>
Date:   Mon Sep 14 23:17:53 2015 -0400

    pluto: sprinkle const on struct spd_route and related tidying
   [...]
    - remove redundant const from struct_desc objects

only to then find, on the next iteration, that they can't be const so
we get to put them back.  Or we end up with the tacky workarounds like
this:

include/constants.h:typedef const struct enum_names enum_names;
include/constants.h:typedef const struct enum_enum_names enum_enum_names;
include/constants.h:typedef const struct sparse_name sparse_names[];

suck it up, drop the typedef, it's a struct.


More information about the Swan-dev mailing list