QuEP 4: Reducing abstraction penalty by means of disposable arrays

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

QuEP 4: Reducing abstraction penalty by means of disposable arrays

DU VIGNAUD François

 

Hi all,

 

I have found something that seems to be a bug in the disposable Disposable Class Template. Indeed, the following constructor destroys the non disposable object t given as argument: (<a href="mk:@MSITStore:C:\Program%20Files\Quantlib\QuantLib-0.3.12\Docs\QuantLib_cpp-3.12.chm::/disposable_8hpp-source.html">ql/Utilities/disposable.hpp line 74 to 77)

    template <class T>

    inline Disposable<T>::Disposable(T& t) {

        this->swap(t);

    }

 

To my point of view this behaviour is not consistent with the original Luigi’s proposal. By the way, this “bug” is corrected in the code excerpts featured in the QuEP9.

 

Best regards,

 

François

 

Reply | Threaded
Open this post in threaded view
|

Re: QuEP 4: Reducing abstraction penalty by means of disposable arrays

Luigi Ballabio
On 05/16/2006 07:52:59 PM, DU VIGNAUD François wrote:
> The following constructor destroys
> the non disposable object t given as argument:
> (ql/Utilities/disposable.hpp)
>
>     template <class T>
>     inline Disposable<T>::Disposable(T& t) {
>         this->swap(t);
>     }

Yes, this is by design. The purpose is to allow one to write

Disposable<Foo> bar() {
     Foo f(1,2);
     return f;
}

and avoid the copy in the automatic conversion from Foo to  
Disposable<Foo> implied by the return. The problem was that it is  
difficult to initialize a Disposable directly, as in:

Disposable<Foo> baz() {
     Disposable<Foo> f(1,2);
     return f;
}

because Disposable has no idea of how many arguments the constructor  
should take, nor of their types. Other approaches might be possible,  
but I think they make the resulting client code more clumsy.

Luigi


----------------------------------------

The first rule of intelligent tinkering is to save all the parts.
-- Paul Erlich