#177 SmartPtr: destructive copy from an rvalue is not working


SmartPtr with DestructiveCopy ownership policy can't perform copy construction from an rvalue.

#include <Loki/SmartPtr.h>

typedef Loki::SmartPtr<int, Loki::DestructiveCopy> Ptr;

Ptr makePtr() {
return Ptr(new int);

int main() {
Ptr ptr = makePtr(); // error!

Constructor taking RefToValue seems to be never called because template constructor with const argument is favoured. Also there is no conversion between RevToValue holding reference to SmartPtr with different pointee types.

It seems that the desired behaviour could be achieved by Colvin-Gibbons trick used in std::auto_ptr. However, it's more difficult to implement it in this SmartPtr. I've tried to write my own more lightweight policy-based SmartPtr and came up to some code which I attach here. It's not finished yet, but DestructiveCopy seems to be working.


  • Anonymous

    My example of SmartPtr with DestructiveCopy

    • assigned_to: nobody --> rich_sposato
    • status: open --> open-accepted
  • Added tests for DestructiveCopy policy. One test is commented out until this bug gets fixed.

    I did confirm compiler calls wrong constructor when SmartPtr is returned by value - thus leading to bug described here.

    revision 1097.

    • status: open-accepted --> closed-duplicate
  • This is a duplicate of bug 2080889.

    Closing this bug report so only one report exists for this problem.

  • Fixed bug by adding const overload of Clone function in DestructiveCopy policy class.

    Fixed in revision 1101.
    Test for fix is available in revision 1102.