From: Alejandro Colomar <alx@kernel.org>
To: gcc-help@gcc.gnu.org
Subject: Re: Assignment of union containing const-qualifier member
Date: Wed, 31 Jan 2024 19:14:14 +0100 [thread overview]
Message-ID: <ZbqN9qlQ6CpwUU66@debian> (raw)
In-Reply-To: <Zblt7e55N1FPWfUM@debian>
[-- Attachment #1: Type: text/plain, Size: 2311 bytes --]
On Tue, Jan 30, 2024 at 10:45:11PM +0100, Alejandro Colomar wrote:
> Hi,
>
> I'm trying to do something like the following:
>
> $ cat const.c
> union u {
> int a;
> const int b;
> };
>
> int
> main(void)
> {
> union u u, v;
>
> u.a = 42;
> v = u;
> }
> $ cc -Wall -Wextra const.c
> const.c: In function ‘main’:
> const.c:12:11: error: assignment of read-only variable ‘v’
> 12 | v = u;
> | ^
> const.c:9:21: warning: variable ‘v’ set but not used [-Wunused-but-set-variable]
> 9 | union u u, v;
> | ^
>
> The actual data I'm using is not just an int, but that serves to
> reproduce the problem easily. In reality, the union is more like this:
>
> struct rstr {
> const size_t length;
> const char *const start;
> };
>
> union str {
> struct {
> size_t length;
> char *start;
> } w;
> struct rstr r;
> };
>
> I don't see anywhere in C11 that makes this a constraint violation, and
Ahh, I didn't find it. It's specified undex 6.3 (Conversions),
6.3.2.1 (Lvalues, arrays, and function designators)
<http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1>:
< A modifiable lvalue is an lvalue that does not have array type, does
< not have an incomplete type, does not have a const- qualified type,
< and if it is a structure or union, does not have any member
< (including, recursively, any member or element of all contained
< aggregates or unions) with a const- qualified type.
I'm wondering if this is a bit conservative, and a union like this could
be a modifiable lvalue. I find it useful, since it allows inheriting a
non-modifiable version of the string which cannot be made modifiable
again, but would let you edit the string as long as you keep the union.
I added the named member 'w' to allow copying v.w = u.w, but when the
union is part of a larger structure and I need to copy the entire
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?
Cheers,
Alex
--
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-01-31 18:14 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 [this message]
2024-02-04 7:33 ` Amol Surati
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=ZbqN9qlQ6CpwUU66@debian \
--to=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).