From nobody Thu Feb 20 11:08:10 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 4Yz9V42YrRz5nkB6 for ; Thu, 20 Feb 2025 11:08:12 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (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 4Yz9V41tfMz40sF; Thu, 20 Feb 2025 11:08:12 +0000 (UTC) (envelope-from dim@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740049692; 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=xtYuWM8kHO4Azypm0oV1m/K8k+rs8m7tCfstlKRhJyM=; b=RgeJ/T5YzmMUxNlN7CD+sHH3KZUSsARQv1SRaDOypGIqByqQqaA0xEqIfzzO5wYBVT8fgj 4OCoVekRJrroK8Ihmjim6S3bfMX6xypQwWAvmI39XS/95KSFtb4g5LZeJ6RaZj3Y01Q09g FNFgw9PMKykdflc11OS8Gc/9Ax1+N/xfnAQA9FzEpqWAo6I8U0cTFrE8RQ96U0TykWfaBi KRVJ/Op96eijgRodlxlH5iAO2QG3B6Ou9Wmsy6EjlcZq+jBhHmCtxqN3wYI8cDVyNnAdXD lr4MsNBEZaAt7F++mo8TEhI0xxFs9CRAciB0QmYS105hZ8B53PeWTO397af/Ag== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1740049692; a=rsa-sha256; cv=none; b=raIDz0MVIqizx/O5rJjyfOOqBQOJ5eU4wDLVA5h1slCiI3HM0fvOAQZ+1/M3bLNVpIHkOO UI9zKDxdqnCEvxi6yeuO/JO+0/PTra+HXYWuZr5+LWTDlFTm9s/OOjRXLdDEtkRgdoiH10 Gpt48mv2cEMvXWuyZhanXTGa3ELzviEdQmz/o25othZvEhwIM/AJyGiUrFI261cpCCqDMd 0zn67u5WMfh6EJlkwWr4FBQaszJJIJhGZ/KeruaBURNndK9FPNQj/PA9kCH3UFOutWaEMS zXbIva/0yep0ncOnR88ZMYWi3pgVDXvjg7syOGq0YEvJkMX+p+SYRpSEE43/EQ== 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=1740049692; 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=xtYuWM8kHO4Azypm0oV1m/K8k+rs8m7tCfstlKRhJyM=; b=vulQQm8CGRAU2wGFz9mZbzgT+325Sc0iVoYQkElNF4KwRr5i2kD+nMTFHrix1yenL6CeHL 2lDuDoJ1fW1akyFUtJB0hGH9KEAaEaeL7fvosylvbPEOLgUcJTJEOO4CiPebzIh4f6dMef CBpbV/ss8/rwJdqUsWtWz0kr5C+iJAcxzqsERyteypmotv9ZGHUo6nxVbTc405L8VSxIkz 5kPTSeQNkVw3xetNcIUJ5muMavtRgNuMCrpiJ0IOKxn1js/yIpyVOVb3b+88/0fcEmFwM0 nhKGpuoYpP2I7BUYCzCOighuS1W8/tsCKtHTzBguzrCthnYg/wN2Hl02mNvEMw== Received: from tensor.andric.com (tensor.andric.com [87.251.56.140]) (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 (2048 bits) client-digest SHA256) (Client CN "tensor.andric.com", Issuer "R10" (verified OK)) (Authenticated sender: dim) by smtp.freebsd.org (Postfix) with ESMTPSA id 4Yz9V40Yxvzqdg; Thu, 20 Feb 2025 11:08:12 +0000 (UTC) (envelope-from dim@FreeBSD.org) Received: from smtpclient.apple (longrow.home.andric.com [192.168.0.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 8251F56932; Thu, 20 Feb 2025 12:08:10 +0100 (CET) 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 \(3731.700.6.1.9\)) Subject: Re: Empty structures have sizeof(1) in C++ now ? From: Dimitry Andric In-Reply-To: <9A5B431C-2405-412A-90FF-DA016E80DF68@FreeBSD.org> Date: Thu, 20 Feb 2025 12:08:10 +0100 Cc: current@freebsd.org, David Chisnall Content-Transfer-Encoding: quoted-printable Message-Id: <9AD91F33-8A44-480C-9DAB-766D6C0228B5@FreeBSD.org> References: <202502200940.51K9eswS003142@critter.freebsd.dk> <9A5B431C-2405-412A-90FF-DA016E80DF68@FreeBSD.org> To: Poul-Henning Kamp X-Mailer: Apple Mail (2.3731.700.6.1.9) In fact, in C having a struct without members is undefined behavior: > If the struct-declaration-list does not contain any named members, = either directly or via an anonymous structure or anonymous union, the = behavior is undefined. However, gcc allows this as an extension, and clang has apparently = followed that: https://gcc.gnu.org/onlinedocs/gcc/Empty-Structures.html -Dimitry > On 20 Feb 2025, at 12:03, David Chisnall wrote: >=20 > 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). >=20 > 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 >=20 > This lets you do things like: >=20 > ```c++ > template > struct SomeStructThatMayContainAnother > { > // Normal fields go here >=20 > [[no_unique_address]] > std::conditional_t, struct {}, = Embedded> embeddedStruct; >=20 > // More fields maybe here > }; > ``` >=20 > In this case, `embeddedStruct` will not add any space to the parent = struct if the template parameter is `void`. >=20 > David >=20 >> 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 >=20 >=20