Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news.alt.net From: James Kuyper Newsgroups: comp.std.c++ Subject: Re: Is a union a kind of class? Date: Fri, 27 Dec 2013 13:11:16 CST Organization: Self Lines: 35 Sender: std-cpp-request@vandevoorde.com Approved: austern@google.com Message-ID: <52B84DE2.2070604@verizon.net> References: <52204496.5050408@bawi.org> <24874a99-32a8-475d-88c2-36631dfb45ba@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 To: undisclosed-recipients:; Return-Path: X-Original-Date: Mon, 23 Dec 2013 09:51:14 -0500 X-Submission-Address: std-cpp-submit@vandevoorde.com Xref: csiph.com comp.std.c++:700 On 12/22/2013 08:27 PM, news wrote: > > On 12/19/13 13:24, johnolithicsoftware@googlemail.com wrote: > [snip] > >> The size of a union is equal to the size of it's largest data member >> if I'm not mistaken. > > What about: > > union dbl_charray > { > double dbl; > char [sizeof(double)+1] charray; > }; > > IIUC, in order to satisfy the alignment requirements of the double, > the size would have to be 2*sizeof(double), not just sizeof(double)+1. Not necessarily. The minimum possible size will be sizeof(double) + alignof(double), which could be smaller than 2*sizeof(double) - in principle, it could be as small as sizeof(double) + 1. The actual size could be larger than the minimum - some implementations give all classes the same alignment requirement, which would be alignof(max_align_t), and could in principle be larger than 2*sizeof(double), though it's rather unlikely. -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]