From nobody Thu Feb 20 11:03:28 2025 X-Original-To: current@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Yz9Nz4Wgpz5njlK for ; Thu, 20 Feb 2025 11:03:47 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Yz9Nz1hnCz3vxJ; Thu, 20 Feb 2025 11:03:47 +0000 (UTC) (envelope-from theraven@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740049427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yF/D25t/FlzN714fNAU31LdlXyX9sXuntdNBIhSah08=; b=JHX4ayY0dbD9ADeMsBqzrPmVk8pOKwRy/UVihvKvpeDw/oe77do4An7kY2OIvqptNFtDm1 NxZ/l1t3SgHv1nFb6vFyixyf9XtcsI71+Tw9st1YhBW2IdPMI1J55U1zQg9jjUS8si6KL5 8BFTiTdOGfWInWm11cYNKqO8XWcVDt0/i+lpmC6ZY/6SrgMQOcNfVbOYegGAbwkPaqYAPm GRoVvaZMPalKP8KVQXKerHkqkPIXhJOU4eCEyhJNe+yJ7EkHY+SmEixwpdGaFIufd4/2Zk bZQVEDG9zqB6CO0fyodJ/dO/sVclqThtUJx24YkTT8js/QOZYd+NkwHfNljTVw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1740049427; a=rsa-sha256; cv=none; b=tgaKOyH2Ns0X1WObYdzxTseJrSbEwxIF/Sw2zVU79EXTZ5l7RpzcBt/Z51fGywyyY1QNe3 U5CKMUbXjm5wS4/mX1dQK83+EtLcsuy38WRHk+q2woSvhb+TKf9x44IKjEx+hh0BLEObMw WFSoX1y3WrPk8MvfpoqwIyOv7Db4WzHR7qCGBgpAmmmHea0j0ZLeJjSGjUGf0doRuByKrI KOLMcSdp1v/ON5Y+T/tOeS+bhyQpS8o/B3uM9Bq+4inE1NEYnv9sGN92WwEL+faUYUUqZn J7EojoDO9X5gdx32hUaPraB20P1y3PeM3pUCw4tBeEt0C8Xm1tYlXBkTLWQy2w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740049427; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yF/D25t/FlzN714fNAU31LdlXyX9sXuntdNBIhSah08=; b=Ar4JieUJS+j711Mhl1mxFhRNm2X/vtcGW0d2YMhFeG9xtOMrS+HSJlL5ikrbc6ZcffhcBB XTX3zISzoSvbup1sgSqzMfedA8S40M7vy9oMd6JzDZPWJ7j+R96s261/91rKLVicAaUmAy aPMQ6eedA5MJBT6DJx7dSIumlG2Tcquln7u3g/p+QcG8b3/cafWsnFlVqqV7gbKHSl8btC mKs38KbIqibmgz+QlVdTktW5K7pGzTAqjkjLuKfi3eVGSUbd2yjenVIckDsy1D89Q0N4VN rY4YP1kPhVERSy8HX/FxiTYdMmmFcetH6PHInKpiztkwGzkxEVetTTbPwLwYoQ== Received: from smtp.theravensnest.org (smtp.theravensnest.org [45.77.103.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: theraven) by smtp.freebsd.org (Postfix) with ESMTPSA id 4Yz9Nz1FdnzqK2; Thu, 20 Feb 2025 11:03:47 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from smtpclient.apple (unknown [194.168.166.4]) by smtp.theravensnest.org (Postfix) with ESMTPSA id 22608D8; Thu, 20 Feb 2025 11:03:46 +0000 (GMT) Content-Type: text/plain; charset=utf-8 List-Id: Discussions about the use of FreeBSD-current List-Archive: https://lists.freebsd.org/archives/freebsd-current List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-current@FreeBSD.org Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51.11.1\)) Subject: Re: Empty structures have sizeof(1) in C++ now ? From: David Chisnall In-Reply-To: <202502200940.51K9eswS003142@critter.freebsd.dk> Date: Thu, 20 Feb 2025 11:03:28 +0000 Cc: current@freebsd.org Content-Transfer-Encoding: quoted-printable Message-Id: <9A5B431C-2405-412A-90FF-DA016E80DF68@FreeBSD.org> References: <202502200940.51K9eswS003142@critter.freebsd.dk> To: Poul-Henning Kamp X-Mailer: Apple Mail (2.3776.700.51.11.1) No, that=E2=80=99s always been the case in C++. It comes from the rule = that two allocations must have unique addresses. If a structure could = have size zero, an array of these structures would have size zero and = the two elements in the array would have the same address. Similarly, = two struct fields could have the same address, which breaks other bits = of the language (pointers to members would compare equal when they = should not). C++20 introduced the no_unique_address attribute. This allows you to = embed a struct in another and, if the child struct has no fields, then = its address is not required to be unique and you are explicitly saying = that you won=E2=80=99t do any of the things where this would be a = problem. =20 This lets you do things like: ```c++ template struct SomeStructThatMayContainAnother { // Normal fields go here [[no_unique_address]] std::conditional_t, struct {}, = Embedded> embeddedStruct; // More fields maybe here }; ``` In this case, `embeddedStruct` will not add any space to the parent = struct if the template parameter is `void`. David > On 20 Feb 2025, at 09:40, Poul-Henning Kamp = wrote: >=20 > Is this a bug ? >=20 >=20 > critter phk> cat /tmp/_.c >=20 > #include > struct foo { > }; >=20 > int > main(int argc, char **argv) > { > struct foo bar; > printf("%jd %jd\n", sizeof(struct foo), sizeof(bar)); > return (0); > } > critter phk> cc -o /tmp/a.out /tmp/_.c > critter phk> /tmp/a.out > 0 0 > critter phk> c++ -o /tmp/a.out /tmp/_.c > c++: warning: treating 'c' input as 'c++' when in C++ mode, this = behavior is deprecated [-Wdeprecated] > critter phk> /tmp/a.out > 1 1 >=20 > --=20 > Poul-Henning Kamp | UNIX since Zilog Zeus 3.20 > phk@FreeBSD.ORG | TCP/IP since RFC 956 > FreeBSD committer | BSD since 4.3-tahoe > Never attribute to malice what can adequately be explained by = incompetence. >=20 >=20