From 8805827845985510339 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,855073145fb9fdb7 X-Google-Attributes: gidf78e5,public From: ncm@nospam.cantrip.org (Nathan Myers) Subject: Re: Minimum Size of Array in Standard (solution) Date: 1998/11/11 Message-ID: <72ak12$ma2$1@shell7.ba.best.com>#1/1 X-Deja-AN: 410521886 Approved: stephen.clamage@sun.com (comp.std.c++) References: <199811032027.NAA25799@ncar.ucar.EDU> <71p6m5$4m1$1@shell7.ba.best.com> <72a0m6$o75$1@news.lth.se> Organization: http://www.cantrip.org/ X-UID: 0000000001 X-Status: $$$T Newsgroups: comp.std.c++ Originator: clamage@taumet Hans Olsson wrote: >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. > > why not plain T dummy ? Because a single T might not be big enough. >Why not have a template-union instead of hiding the union inside >a struct? The point is that the varstruct probably has useful things in it. Generally it has more than just an unsigned. Template tricks can be used to make this technique prettier, but [I hope] you don't use it often enough to make that important. Note that the above is only appropriate if T is a POD. -- Nathan Myers ncm@nospam.cantrip.org http://www.cantrip.org/ [ 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 ]