From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x134.google.com (mail-lf1-x134.google.com [IPv6:2a00:1450:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id 4F5BD3858C60 for ; Sun, 4 Feb 2024 07:34:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F5BD3858C60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4F5BD3858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707032051; cv=none; b=i3sf+QpmDZbLcahcxMUeHYWVfQbohtKZSonRPO2N95BYRu7IMuVeLIKDQ1U1geXOHbk0/vm8byERPuS+K5LPakQL1j5/AEHxmzi34vPz/q9riYrcx5HwlHTjnirI4gSUe2qn+NMiTK16EALWSQ1zJHIHDJirTdWaYrv57YeKxEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707032051; c=relaxed/simple; bh=1PqHLkPXXe64PQI1kEAL9eiBuXKY3wZPCIE+tVQIsNM=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=myXm1HdPWue23ZyKfynKIb47fbjDcyDO/aykcPrUBvKlmy7Z3BZDgQeiN5lOpdF4JoVSSv2KyCc56yQcgcQZwwdwX7kP5mWwpmgFL3+oCToSLdRjVO3kN5h/S4kQFNBK8bCkunP7Fz6ditoZCHCzlWlocUbWs1AknEzZL4ZhBxc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-5114cd44f6aso130590e87.1 for ; Sat, 03 Feb 2024 23:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707032047; x=1707636847; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ZfjiOOdnQ8c9tu+sEoF10xCAfWxrhl3lYRw5UOS0uMM=; b=JaKgCL1RzG5512gmEQ/8ANUadvPt7+uw4p6d+cjxab71it2Q86bW8kweO73j8flSMM Eb+VamOWe5T2aG1jpDyTwSG9IinUrICxFatGNO8yysucbDIG7D6uWPOKAxurAARt6WSp EAl/h3AtROFtWgNoHpe3Q09+TJ38H2FcZwesgFcV4rCAkX/w0J2+uaJj9mxykiFjlqat ITBgKYh+NOgNT4u4i2V/oY4PIwQJ1h/jPusWwkt8kT1fAXghJHpLXjQ6iqH/QdpYE+hf jKFG+i0N3+I2B0aZz98t0KD2lE38IVgsnA1d5gpBlDyAhs1ZILwIo+Yg6Tb95BmSoBQd as2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707032047; x=1707636847; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZfjiOOdnQ8c9tu+sEoF10xCAfWxrhl3lYRw5UOS0uMM=; b=iXNcTPNjxpQAQlb7A1fo1S125jGpbEpCDfXiHZn5e4mEo206tL2TsrdsQl+FVTX22t amn1zfvYwEKgKQtqfeecmpsNKaymIh7egm4GZ3cH3IPhCJpAf4G18mVtsG+xjP2/2TSg 532CuBMG52XAFTuZQAgxSiwOz28Xhb03AIUNUcNUUrLSKnJN5gcbO3tSoHQMfIvB6MnI JlMSnZFGEHsMO14YTLw3Q24xRK96cs0xZugJZ1bgr80bgLNuvPGYXBV+aYDMpfFfS5Z8 vvjYAov+6ifqhjd89KZfrR98QZFir+OzZ8/HBX2pvGCyGZQkGBtEg4TUfMKOJoeL82Af +tCA== X-Gm-Message-State: AOJu0YwSzgHmZjw5FVtrpvPVfy9q/AexMghCHowIaS02vZlp5g9R1vRI VmluTNCCRl4cCtavPTGrlQQ+xWLIdW170dL0L/A32MaY56o63zr8rCuBPlu7OZRFcKOO0HE9LpF pOC7O/z1u+ncAb6/eBP98+S6vHg0= X-Google-Smtp-Source: AGHT+IGltK1WVkNJ65eJpgu213MWM8PJiYl8M8VJuQYESdgRm8oIz2+EZcGBBeHiiwNxQcvfswntvHlq7azpY2P7UJE= X-Received: by 2002:a05:6512:3a85:b0:511:4c51:d18e with SMTP id q5-20020a0565123a8500b005114c51d18emr644218lfu.4.1707032047298; Sat, 03 Feb 2024 23:34:07 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Amol Surati Date: Sun, 4 Feb 2024 13:03:48 +0530 Message-ID: Subject: Re: Assignment of union containing const-qualifier member To: Alejandro Colomar Cc: gcc-help@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 31 Jan 2024 at 23:46, Alejandro Colomar via Gcc-help 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 > > -- > > Looking for a remote C programming job at the moment.