From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25693 invoked by alias); 11 Sep 2014 08:11:08 -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 25682 invoked by uid 89); 11 Sep 2014 08:11:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.1 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 11 Sep 2014 08:11:06 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s8B8B5Eo017881 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 11 Sep 2014 04:11:05 -0400 Received: from zebedee.pink ([10.3.113.10]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s8B8B36p027014; Thu, 11 Sep 2014 04:11:04 -0400 Message-ID: <54115917.1040602@redhat.com> Date: Thu, 11 Sep 2014 08:11:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.8.0 MIME-Version: 1.0 To: haynberg@yahoo.com, "gcc-help@gcc.gnu.org" Subject: Re: is portable aliasing possible in C++? References: <1410390231.39617.YahooMailNeo@web140202.mail.bf1.yahoo.com> In-Reply-To: <1410390231.39617.YahooMailNeo@web140202.mail.bf1.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00069.txt.bz2 On 11/09/14 00:03, haynberg@yahoo.com wrote: >>> So it would seem the answer is no, portable aliasing is not possible >>> in C++. Though I wonder if it should be (with a new language >>> feature). >> >> In practice, a union is exactly that, and I've not heard of a compiler >> which doesn't do the right thing. Given that it's explicitly legal in >> C11, I can't see any reason which C++ shouldn't simply adopt the same >> language. > > I agree. But in the union case, you're copying bytes. If you don't perform > > a copy, for example: > > struct msg { > // ... > }; > char *get_bytes(); > msg *p = reinterpret_cast(get_bytes()); Why are you doing this? > if (p->i) > // ... > > Then there can't be a portable way to do this, because there's hardware that > doesn't permit unaligned reads (e.g. where you'd get a SIGBUS). But a union will always be correctly aligned for all members. Andrew.