From 1512491866652752474 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,855073145fb9fdb7 X-Google-Attributes: gidf78e5,public From: Email_To::.Hans.Olsson@dna.lth.se (Hans Olsson) Subject: Re: Minimum Size of Array in Standard (solution) Date: 1998/11/10 Message-ID: <72a0m6$o75$1@news.lth.se>#1/1 X-Deja-AN: 410406995 Content-Transfer-Encoding: 8bit Approved: Fergus Henderson References: <199811032027.NAA25799@ncar.ucar.EDU> <71p6m5$4m1$1@shell7.ba.best.com> X-Original-Date: 10 Nov 1998 18:29:26 GMT Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: news@news.unimelb.edu.au X-Trace: izvestia.its.unimelb.edu.au 910724967 26280 128.250.29.16 (10 Nov 1998 19:09:27 GMT) Organization: Lund Institute of Technology, Sweden X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANkiPVuEDnX0m9pzZAQEVRAF/Vpyk065I8ZmcqQwGinCd3byLYrmvBRKY 3jw159nBlPrwotFBO2U7XbSSEeYyc+RT =gdyd Mime-Version: 1.0 NNTP-Posting-Date: 10 Nov 1998 19:09:27 GMT Newsgroups: comp.std.c++ In article <71p6m5$4m1$1@shell7.ba.best.com>, Nathan Myers wrote: >There is a standard-conforming way to get a similar effect. > > template > struct varstruct > { > union { > unsigned int num_elements; > T dummy[(sizeof(unsigned int)+sizeof(T)-1)/sizeof(T)]; > }; > T* elements() { return (T*)(char*)(this+1); } > }; > >The dummy array ensures that (this+1) is aligned properly for a T. Apart from the other questions (i.e. why not plain T dummy;): Why not have a template-union instead of hiding the union inside a struct? -- // Home page http://www.dna.lth.se/home/Hans_Olsson/ // Email To..Hans.Olsson@dna.lth.se [Please no junk e-mail] --- [ comp.std.c++ is moderated. To submit articles, try just posting with ] [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]