Boost logo

Boost :

Subject: Re: [boost] [container] varray aka static_vector
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2013-02-13 11:51:45


2013/2/13 Nevin Liber <nevin_at_[hidden]>

> On 13 February 2013 07:20, Krzysztof Czainski <1czajnik_at_[hidden]> wrote:
>
> > Thank you, Nevin, for the above example. I think I see your point now.
> > Furthermore, it gives me an idea:
> > template < class T >
> > struct MyAllocator : std::allocator<T>
> > {
> > using std::allocator<T>::construct;
> >
> > // for v.emplace_back(noinit);
> > void construct( T* c, noinit_t/*,
> > typename boost::enable_if< boost::is_pod<T> >::type* = 0*/ )
> > {}
> >
> > };
> >
>
> I wouldn't even bother with the enable_if; just use the default
> initialization placement new syntax when no arguments are passed, as in:
>
> // direct initialization
> template<typename... Args>
> void construct(T* c, Args&&... args)
> { ::new (static_cast<void*>(c)) T(std::forward<Args>(args)... ); }
>
> // default initialization for zero arguments
> void construct(T* c)
> { ::new (static_cast<void*>(c)) T; }
>

Oh yeah, that will just do the right thing in all cases, thanks ;-)

But I think you missed the noinit_t trick above, didn't you?

> Note: there is a bit more work that has to be done for allocators than
> just deriving from std::allocator<A>; for instance, rebind won't do the
> right thing.
>

Oh, thanks, I'll try if that fixes my compiling errors and post results
later.

> I don't think this can be done for resize(), can it?
>
> Works for resize too under C++11.
>

I mean the noinit_t trick - can that be done for resize just by writing an
Allocator? I don't see how.

Regards,
Kris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk