linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@osdl.org>
To: Al Viro <viro@ftp.linux.org.uk>
Cc: Andrew Pinski <Andrew_Pinski@PlayStation.Sony.Com>,
	Derek M Jones <derek@knosof.co.uk>,
	Linux/PPC Development <linuxppc-dev@ozlabs.org>,
	linux-sparse@vger.kernel.org,
	Geert Uytterhoeven <Geert.Uytterhoeven@sonycom.com>
Subject: Re: cast truncates bits from constant value (8000000000000000 becomes 0)
Date: Fri, 1 Dec 2006 13:19:27 -0800 (PST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0612011309480.3695@woody.osdl.org> (raw)
In-Reply-To: <20061201210011.GE3078@ftp.linux.org.uk>



On Fri, 1 Dec 2006, Al Viro wrote:
> 
> "C++ way of doing things" is hardly an endorsement.  _IF_ we are changing
> the way enum works, might as well do it sanely and have the type of
> enumeration constant same as that of expression initializing it (with
> usual implicit initializers).  But yes, that explicitly changes semantics -
> enum { A = 0L, B, C }; will have A, B and C long, not int.  Direct
> contradiction with C90/C99...

Well, the C++ definition at least means that enumerated names have the 
same types _after_ the enumaration is closed (ie the example I sent out 
will at least give the same value for the sizeof() of everything).

So the C++ definition is a lot saner than what gcc does now.

On the other hand, I do agree that "keeping the enumerated type the same 
as the initializer expression type" is _also_ a sane situation. And it's 
better than the C++ situation in the sense that at least the sizes are 
_consistent_ (which C++ is not - the size AT DEFINITION time is 
potentially different from the size AFTER the definition).

So in the C++ world, you have the odd situation that

	enum strange {
		one = (char) 1,
		other = sizeof(one),
	};

we will actually end up with

	other != sizeof(one)

AFTER the declaration (because when "other" got its value, "one" had a 
type of "char", but _after_ the declaration, "one" will have a type of 
"size_t" or "int" or whatever, because the final type of "one" depends on 
the type of the enumerator.

So the C++ definition is really odd, but it's better than what gcc does, 
because at least all the values end up having a common type at the end.

The version where all the values _keep_ their types all the time, and you 
can force them to be whatever you want (by just making sure the 
initializer expression has the right type) is the most flexible one and at 
least doesn't have inconsistencies like the above example, but it's 
neither what gcc nor the C++ standard (or any older C standard, for that 
matter) actually says.

Anyway, because of all this, enum types are a mess. sparse warns if you 
can't fit it in an "int", which is the traditional and fairly dependable 
old C behaviour, and basically says "extended integer types for enums are 
flaky as hell - warn about them!"

		Linus

  reply	other threads:[~2006-12-01 21:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-22  4:20 [PATCH 8/14] powerpc: add ps3 platform repository support Geoff Levand
2006-12-01 14:25 ` cast truncates bits from constant value (8000000000000000 becomes 0) Geert Uytterhoeven
2006-12-01 14:39   ` Al Viro
2006-12-01 14:55     ` Geert Uytterhoeven
2006-12-02  7:50       ` Michael Ellerman
2006-12-01 15:30   ` Linus Torvalds
2006-12-01 16:51     ` Geoff Levand
2006-12-01 20:20       ` Linus Torvalds
2006-12-01 20:49         ` Derek M Jones
2006-12-01 21:00           ` Al Viro
2006-12-01 21:19             ` Linus Torvalds [this message]
2006-12-01 20:50         ` Linus Torvalds
2006-12-01 21:17           ` Geoff Levand

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=Pine.LNX.4.64.0612011309480.3695@woody.osdl.org \
    --to=torvalds@osdl.org \
    --cc=Andrew_Pinski@PlayStation.Sony.Com \
    --cc=Geert.Uytterhoeven@sonycom.com \
    --cc=derek@knosof.co.uk \
    --cc=linux-sparse@vger.kernel.org \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=viro@ftp.linux.org.uk \
    /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 NNTP newsgroup(s).