Posted by
Michael Sharpe on
May 31, 2013; 4:01am
URL: http://quantlib.414.s1.nabble.com/c-0x-tp14280p14300.html
Hi Peter,
First off, thanks for taking a look at getting C++11 into a major project!
The line
Clone(std::unique_ptr<T>& p);
looks suspicious to me. It doesn't make it clear from the client side that the pointer ownership was changed. Compare the following three examples:
// example 1
std::unique_ptr<T> ptr(new T());
Clone<T> newObject(ptr);
// example 2
std::unique_ptr<T> ptr(new T());
Clone<T> newObject(std::move(ptr));
//example 3
Clone<T> newObject(std::unique_ptr<T>(new T()); // 3
Example 2 makes it obvious that the constructor will retain ownership of the unique_ptr, which isn't obvious in example 1 even though the current implementation means example 1 and example 2 are the same. In addition, the current code will remove the ability for clients to use code like example 3 completely, as you can't bind a temporary object to a non-const lvalue reference. It's possible clients could run into problems even if they don't upgrade from auto_ptr to unique_ptr if they have code like this.
I propose that, instead of changing the function definition to incorporate either auto_ptr or unique_ptr, that another Clone constructor is added, so that there will be two constructors:
Clone(std::auto_ptr<T> ptr);
#ifdef QL_UNIQUE_PTR_ENABLED
Clone(std::unique_ptr<T> ptr);
#endif
The implementation of the auto_ptr constructor will need to make sure to initialize the unique_ptr calling uniquePtr(std::move(autoPtr)) in the initializer class if the internal class uses unique_ptr. This is because unique_ptr has a constructor that accepts auto_ptr by rvalue reference only, and rvalue references cannot bind to lvalues. But, you can base the private data stored in clone off object internals simply off the QL_UNIQUE_PTR_ENABLED #define, without clients having to change their code. The decision to remove the auto_ptr constructor can be made later.
Hopefully this helps. Let me know if an updated Clone.hpp would make things a little bit clearer and I can put something together when I have some time.
Mike
------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite
It's a free troubleshooting tool designed for production
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap2_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev