|
Boost : |
Subject: Re: [boost] [container] varray aka static_vector
From: Nevin Liber (nevin_at_[hidden])
Date: 2013-02-13 10:46:08
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; }
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.
I don't think this can be done for resize(), can it?
>
Works for resize too under C++11.
-- Nevin ":-)" Liber <mailto:nevin_at_[hidden]> (847) 691-1404
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk