|
Boost : |
From: Anthony Liguori (anthony_at_[hidden])
Date: 2003-05-19 19:59:22
>
>
>That's not what I mean. I mean, "I give you the blob of memory, you
>put an object in it", or "if the blob is big enough, put the object
>there". I guess you can do that with allocators, too.
>
>
Out-of-the-box, it is possible to do with the class I have now although
it would require creating a type with a static member that stored where
the preallocated memory.
What I'd rather to though is specialize the class to a placement
allocator and add a constructor that took a void * argument. The usage
would look something like this:
void *my_custom_address = malloc(sizeof(A));
function<A *(int, int, int)> func = ctor_fn<A *(int, int, int),
ctor::placement>(my_custom_address);
A *new_a = func(1, 2, 3); // create object in my_custom_address
Another possible usage could be this:
void *my_custom_address = malloc(sizeof(A));
function<function<A *(int, int, int)> (void *)> func = ctor_fn<A
*(int, int, int), ctor::placement>();
A *new_a = func(my_custom_address)(1, 2, 3);
>>Since the C++ standard does not allow allocators to have any state,
>>
>>
>
>Oh, but it does. They're even encouraged to have state!
>
>
I knew I'd get the standard quoted to me when I said that :-)
>It's just that it also gives the containers permission to treat them
>as though they have no state. Twisty, eh? ;-)
>
>
Yeah, what I meant is that right now, I'm relying on STL-container
allocator semantatics.
>Actually, the containers may only assume there's "one state per
>allocator type", but not that there's no state whatsoever.
>
Sure, I should have said no object-state as class-state is surely allowed.
I never thought of providing placement semantic to this library. I'm
leaning toward my second example to provide this mechanism. Another
thing I began to ponder is giving array semantics so that one could do
the following:
boost::ctor_fn<A *(int, int, int)> func;
A *new_a = func[20](1, 2, 3); // new_a is now an array of 20 elements
Of course, if doing this, there needs to be a corresponding dtor_fn
class so that objects can safely be destroyed...
Regards,
Anthony
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk