From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1893 invoked by alias); 17 Sep 2014 23:57:40 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 1874 invoked by uid 89); 17 Sep 2014 23:57:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-lb0-f173.google.com Received: from mail-lb0-f173.google.com (HELO mail-lb0-f173.google.com) (209.85.217.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Wed, 17 Sep 2014 23:57:37 +0000 Received: by mail-lb0-f173.google.com with SMTP id w7so113901lbi.4 for ; Wed, 17 Sep 2014 16:57:33 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.153.4.39 with SMTP id cb7mr896904lad.19.1410998253500; Wed, 17 Sep 2014 16:57:33 -0700 (PDT) Received: by 10.112.142.7 with HTTP; Wed, 17 Sep 2014 16:57:33 -0700 (PDT) In-Reply-To: <60F6FAE47D1BCE4380CC06D18F49789B93F81CD4@NTXBOIMBX02.micron.com> References: <60F6FAE47D1BCE4380CC06D18F49789B93F81C6E@NTXBOIMBX02.micron.com> <60F6FAE47D1BCE4380CC06D18F49789B93F81CD4@NTXBOIMBX02.micron.com> Date: Wed, 17 Sep 2014 23:57:00 -0000 Message-ID: Subject: Re: Possible bug in gcc 4.4.7 From: Jonathan Wakely To: "Andy Falanga (afalanga)" Cc: "gcc-help@gcc.gnu.org" Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00100.txt.bz2 On 17 September 2014 18:15, Andy Falanga (afalanga) wrote: >> That reinterpret_cast looks dodgy to me, accessing the object through a >> different type is undefined behaviour. What's wrong with doing it >> safely? >> >> Flag operator &=(Flag& f1, Flag f2) { >> unsigned int i = f1; >> i &= static_cast(f2); >> return f1 = static_cast(i); >> } > > Ha, nothing except my thinking that, because I was casting a reference, I had to use reinterpret_cast<>. Well yes, if you want to cast T& to X& and T and X are not related by inheritance then you do need to use reinterpret_cast, but that should make you think "maybe this is not a valid cast, maybe I shouldn't do it" rather than "if I use reinterpret_cast the compiler doesn't complain so it must be the way to go".