|
Boost : |
From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2003-10-01 21:17:32
"Douglas Paul Gregor" <gregod_at_[hidden]> escribió en el mensaje news:20031001122605.P61889_at_monica.cs.rpi.edu...
> On Wed, 1 Oct 2003 cppljevans_at_[hidden] wrote:
>
> > On 10/01/2003 10:52 AM, David Abrahams wrote:
> > > Gregory Colvin <gregory.colvin_at_[hidden]> writes:
> > >
> > [snip]
> > >>>
> > >>> template<typename T> shifted_ptr<T> make_shifted_ptr(cont T& t) {
> > >>> return shifted_ptr(new(so) T(t));
> > >>> }
> > >>>
> > >>> ... make_shifted_ptr(Stuff()) ...
> > >>>
> > >>>are compilers allowed to optimize away the copy and construct the
> > >>>Stuff in place?
> > >
> > [snip]
> > > Oh, wait. You meant the copy of Stuff()? It's not meant to be a T
> > > neccessarily. It's just any old argument to T's constructor. You
> > > need a bunch of overloads for make_shifted_ptr to make the example
> > > complete.
> > >
> > Why couldn't the shifted_ptr(T*) be replaced with
> > shifted_ptr(make_shifted_ptr<T>&), where make_shifted_ptr<T> is
> > simply derived from auto_ptr but has no make_shifted_ptr(T*) CTOR?
> > No copy is needed and it simply turns over the T* to shifted_ptr<T>.
> > Just as David points out, there would need to be a bunch of
> > overloads for every T CTOR or at least those which
> > the user wants to put into shifted_ptr.
>
> Or one could be cute:
>
> shifted_ptr<T> foo(new_<T>(arg1, arg2, arg3...));
>
> Then new_ is a function that returns an object containing the construction
> arguments (the type to construct, T, could even be optional), and
> shifted_ptr is free to do the allocation however it feels.
>
FWIW, this is already supported by Boost.Optional.
The syntax is: in_place(a0,a1,a2), or in_place<T>(a0,a1,a2) for Variant.
The code is already in main CVS: boost/detail/in_place_factory.hpp and typed_inplace_factory.hpp
Fernando Cacciola
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk