|
Boost : |
Subject: Re: [boost] [container] varray aka static_vector
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2013-02-13 18:00:35
2013/2/13 Krzysztof Czainski <1czajnik_at_[hidden]>
> 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
>>
> [...]
> 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 confirm, adding rebind<> fixed my example - it now works both with and
without -std=c++11 (code attached).
> 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.
>
And so the last above question remains ;-) I'll elaborate on that:
struct noinit_t {};
noinit_t const noinit = {};
template < class T >
struct MyAllocator : std::allocator<T>
{
using std::allocator<T>::construct;
// for v.emplace_back(noinit);
void construct( T* c, noinit_t )
{
::new (static_cast<void*>(c)) T; // note: T; instead of T();
}
template < class U > struct rebind { typedef MyAllocator<U> other; };
};
int main()
{
namespace cont = boost::container;
cont::vector< int, MyAllocator<int> > vi;
vi.emplace_back(5);
vi.pop_back();
vi.emplace_back(noinit);
assert( 0 != vi.back() );
vi.pop_back();
vi.resize( 1, noinit ); // !!!
assert( 0 != vi.back() );
}
vi.resize( 1, noinit ) does't compile - and it shouldn't, but my question
is, can I get the desired effect: leave default resize as is, and have an
overload that doesn't zero-initialize PODs?
Cheers,
Kris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk