Re: [libgpiod]: feature request: API functions, const correctness

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Aug 14, 2023 at 10:16 AM Hollerer Franz, Schrack Seconet AG,
Entwicklung <f.hollerer@xxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi Bartosz!
>
> I hesitate to follow your reasoning. I personally highly appreciate const correctness as it IMHO makes the intend of the code clearer, prevents misuse, and gives the compiler more options for warnings and optimization.
>
> Anyway, it's your choice and I respect this.
>
> Franz
>

If your object is:

struct foo {
    int foo;
    unsigned int bar;
};

And you know that after the call to do_stuff(), it will be the same,
then it's perfectly reasonable to go: void do_stuff(const struct foo
*f);.

But if all you have is:

struct foo;

And you have no idea what do_stuff() does internally, then (as the
author of the library) you're limiting yourself for the future if you
suddenly need to do: foo->counter++ in do_stuff().

In C++ this is fine - even if your method is marked as const, you can
have fields marked as mutable if they don't change the "logical
constness" of the object but in C it's better to just not use const
with opaque types.

Bart




[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux