From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-08-20 07:47:30
From: "John Max Skaller" <skaller_at_[hidden]>
> Peter Dimov wrote:
> > > Thats called a C99 VLA. :-)
> > No, it's not. It's called a vector that is as efficient as a VLA.
> > that it's no less efficient than a VLA as long as it's on the stack and
> > resized, and no less functional or efficient than std::vector / new
> > otherwise.
> But such an animal cannot exist. There is no way
> to allocate n elements of an array on the stack with n being
> dynamically determined -- without using VLA's. That is the whole
> point of VLA's.
But I said nothing about a stack or absence of compiler support. :-) I said
'as efficient as', period. There are several ways to meet this requirement,
for example having a lightning fast heap, or having a separate stack from
"the stack," or making std::vector magical.
In short the goal is a std::vector that doesn't give us any excuses to use
_alloca, realloc, new, VLAs, or even scoped_array.
> > > Waste of time. I built one of these, and it is too hard to
> > > use to bother.
> > My experience is somewhat different.
> > class xml_element;
> > class xml_text: public xml_element;
> > class xml_tag: public xml_element;
> This is not a proper variant construction, unfortunately.
> It is a common abuse of inheritance to try to emulate the missing
No, this is not an abuse of inheritance. xml_text IS-A xml_element, and
xml_element is fully abstract. I'm using a 'constrained' variant where the
set of possible types is limited to those derived from a common base.
variant<void> is the uncontsrained variant that accepts anything.
> To make it work, you have to use RTTI, and the result
> isn't safe: you can try to make it safer by providing some
> 'convenience' functionality, but you can't force people
> to use it. :-(
v = 5;
int & ri = variant_cast<int &>(v); // will throw bad_cast when v doesn't
contain an int
if(int * pi = variant_cast<int *>(v))
// we have an int, do something with *pi
// v doesn't contain an int
(I'm using variant<void> in the examples but boost::any works pretty much
the same way.)
> Compare with SML:
> type t = X of int | Y of float
> match e with
> | X x -> print_int x
> | Y y -> print_float y
> where type safety is enforced,
> and, in most cases,
> completeness of decoding checked.
Impossible without language support. :-)
> > > What we really need is core language support :-(
> > I could use LOTS of core language support.
> But you're unlikely to get it.
That was a joke. :-)
-- Peter Dimov Multi Media Ltd.
Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk