From: Amol Surati <suratiamol@gmail.com>
To: Alejandro Colomar <alx@kernel.org>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Assignment of union containing const-qualifier member
Date: Sun, 4 Feb 2024 13:03:48 +0530 [thread overview]
Message-ID: <CA+nuEB9nJo4Z33D5oBM=Ep=k14JvpRxW8u4nWBdAZLgpL7Cm3w@mail.gmail.com> (raw)
In-Reply-To: <ZbqN9qlQ6CpwUU66@debian>
On Wed, 31 Jan 2024 at 23:46, Alejandro Colomar via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> On Tue, Jan 30, 2024 at 10:45:11PM +0100, Alejandro Colomar wrote:
> > Hi,
> >
[ ... ]
> structure, that doesn't help. memcpy(3) does help, but it looses all
> type safety.
>
> Maybe this could be allowed as an extension. Any thoughts?
>
Does it make sense to propose that, if the first top-level member of a
union is completely (i.e. recursively) writable, then a non-const union
object as a whole is writable? If so, then, for union objects a and b of
a union that has such const members, a = b can be expected to not
raise errors about const-correctness.
It seems that a union only provides a view of the object. The union
object doesn't automatically become const qualified if a member
of the union is const-qualified. This seems to be the reason v.w = u.w
works; otherwise, that modification can also be viewed as the
modification of an object (v.r) defined with a const-qualified type through
the use of an lvalue (v.w) with non-const-qualified type - something that's
forbidden by the std.
More towards the use of the string as described:
If there are multiple such union objects that point to the same string,
and if a piece of code decides to modify the string, other consumers of
this string remain unaware of the modification, unless they check for it,
for e.g., by keeping a copy, calc. hash, etc., to ensure that the string was
indeed not silently modified behind their backs.
I think it is better to have a 'class' and associated APIs.
See [1], for e.g., or the implementation of c++ std::string.
The ownership of an object of such a class can be passed by passing
a non-const pointer to the object.
Functions that are not supposed to own the object can be passed a
const pointer. Despite that, if such functions need to modify it for local
needs, they can create a copy to work with.
One can additionally maintain a ref-count on the char pointer, to avoid
having to unnecessarily copy a string if it is going to be placed in several
stay-resident-after-return data-structures.
-Amol
[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3210.pdf
> Cheers,
> Alex
>
> --
> <https://www.alejandro-colomar.es/>
> Looking for a remote C programming job at the moment.
next prev parent reply other threads:[~2024-02-04 7:34 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-30 21:45 Alejandro Colomar
2024-01-31 18:14 ` Alejandro Colomar
2024-02-04 7:33 ` Amol Surati [this message]
2024-02-04 8:18 ` Amol Surati
2024-02-04 18:40 ` Alejandro Colomar
2024-02-04 20:37 ` Alejandro Colomar
2024-02-07 4:13 ` Amol Surati
2024-02-07 13:29 ` Alejandro Colomar
2024-02-12 10:45 ` Amol Surati
2024-02-12 11:18 ` Amol Surati
2024-03-18 9:19 ` Alejandro Colomar
2024-03-18 9:23 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CA+nuEB9nJo4Z33D5oBM=Ep=k14JvpRxW8u4nWBdAZLgpL7Cm3w@mail.gmail.com' \
--to=suratiamol@gmail.com \
--cc=alx@kernel.org \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).