Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add implicit conversion from unique_ptr<T> to T*

General Question: Without going into whether or not it's a good idea, how can I add an implicit conversion operator to a class that has already been defined? For example, let's say that I want unique_ptr<T> to implicitly convert to T*, but I can't just add a member conversion operator because I can't change the definition of the unique_ptr class.

Options:

  1. Is there some c++ voodoo that I can use to make this happen without creating a member function?
    Answer-So-Far: NO.
    There is no way to add an implicit conversion away from a type that you can't modify in code.
    Just ... sadness.

  2. Could I derive from std::unique_ptr and add my own member conversion function? Are there any serious downsides to this?
    Answer-So-Far: Yes (from vsoftco)
    Downsides are yet to be determined. So far inheriting from std::unique_ptr, inheriting its constructors, and declaring an implicit conversion operator has worked splendidly with hardly any code needing to be written.

  3. Am I just going to have to live without this the rest of my life?
    Answer-So-Far: We'll see...
    If I can get option 2 up and running without any serious side-effect or burdens, I'll test it out for a while and report back on whether I think it's worth it. We'll see!

Example code:

#include <algorithm>
#include <memory>
#include <vector>

struct MyClass
{
    MyClass(int v) : value(v) {}

    int value;
};

int main()
{
    auto vec = std::vector<std::unique_ptr<MyClass>>();

    vec.push_back(std::make_unique<MyClass>(1));
    vec.push_back(std::make_unique<MyClass>(2));

    // error C2664: 'void (__vectorcall *)(MyClass *)' : cannot convert argument 1 from 'std::unique_ptr<MyClass,std::default_delete<_Ty>>' to 'MyClass *'
    std::for_each(std::begin(vec), std::end(vec), [](MyClass* myClass)
    {
        myClass->value += 3;
    });
}
like image 327
Isaiah Hines Avatar asked May 31 '15 02:05

Isaiah Hines


People also ask

What is std :: unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

What is an implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What happens to unique_ptr after move?

"Moving" transfers ownership to a new unique_ptr and resets the old unique_ptr .

What is implicit type conversion in C++?

Implicit Type Conversion Also known as 'automatic type conversion'. Done by the compiler on its own, without any external trigger from the user. Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.


1 Answers

If you don't want to use std::unique_ptr<>::get() function, you can:

  1. Define a free function that takes a std::unique_ptr and returns the raw pointer returned by get, although I don't think it really makes your code better, like:

    // free function
    template<typename T>
    T* get_raw_ptr(const std::unique_ptr<T>& up)
    {
        return up.get();
    }
    

    Conversions of unique_ptr to raw pointers are OK, but they have to be explicit. Implicit conversion may lead to lots of headaches, since they may happen when you least expect them.

  2. It is a bad idea to derived from std::unique_ptr, as the latter is not made to be used as a base class (doesn't have a virtual destructor). In general, it is bad to derive from Standard Library classes. However, if you really insist, you can use a wrapper in which you define the implicit conversion operator, like:

    // wrapper
    template <class T, class Deleter = std::default_delete<T>> 
    class unique_ptr_wrapper: public std::unique_ptr<T, Deleter>
    {
    public:
        using std::unique_ptr<T, Deleter>::unique_ptr; // inheriting base ctors
        operator T* () const {return this->get();}
    };
    

and use is simply like

    // wrapper usage:
    unique_ptr_wrapper<int> upw{new int{42}};
    int* p = upw; // implicit conversion OK
  1. 1 and 2 can help you, so you may improve your life ;)
like image 100
vsoftco Avatar answered Oct 21 '22 18:10

vsoftco