Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++x0 unique_ptr GCC 4.4.4

Tags:

c++

gcc

c++11

I am trying to make use of the unique_ptr from C++x0, by doing

#include <memory> 

and comping with -std=c++0x, however it is throwing up many errors this being an example.

/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../include/c++/4.4.4/bits/unique_ptr.h:214: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Descriptor, _Tp_Deleter = std::default_delete<Descriptor>]’

UPDATE**** This is what I am trying to do, I have removed typedefs so you can see clearly the types

static std::unique_ptr<SomeType> GetSomeType()
{
    std::unique_ptr<SomeType> st("Data","Data2",false);

    std::unique_ptr<OtherType> ot("uniportantconstructor data");

    st->Add(ot);

    return st;

}

//Public method of SomeType
  void Add(std::unique_ptr<OtherType> ot)
  {
    //otmap is std::map<std::string,std::unique_ptr<OtherType> >
    //mappair is std::Pair<std::string,std::unique_ptr<OtherType> >
     otMap.Insert(mappair(ot->name(),ot)); 
  }

UPDATE:

If my class SomeType has a method that returns a element from the map (using the key) say

std::unique_ptr<OtherType> get_othertype(std::string name)
{
   return otMap.find(name);
}

that would enure the caller would recieve a pointer to the one in the map rather than a copy?

like image 675
Mark Avatar asked Jan 21 '23 09:01

Mark


2 Answers

std::unique_ptr<OtherType> ot("unimportant constructor data");
st->Add(ot);

You cannot pass an lvalue to a function accepting a unique_pointer, because unique_pointer has no copy constructor. You must either move the lvalue (to cast it to an xvalue) or pass a prvalue:

// pass an xvalue:
std::unique_ptr<OtherType> ot("unimportant constructor data");
st->Add(std::move(ot));
// note: ot is now empty

// pass a prvalue:
st->Add(std::unique_ptr<OtherType>("unimportant constructor data"));

Inside the Add method, things are a little more complicated. First, you have to move from ot, because formal parameters are always lvalues (since they have names). Second, you cannot move from ot and get ot->name() as arguments to the mappair function, because the order of argument evaluation is unspecified in C++. So we have to get ot->name() in a separate statement before moving from ot:

void Add(std::unique_ptr<OtherType> ot)
{
    auto name = ot->name();
    otMap.Insert(mappair(name, std::move(ot))); 
}

Hope this helps. Note that under no (sane) circumstances can two unique_ptr objects point to the same thing. If you need that functionality, unique_ptr is not what you want.

like image 136
fredoverflow Avatar answered Jan 31 '23 09:01

fredoverflow


It looks like you're trying to use the copy constructor. There isn't one. If your calling code looks like this:

T *ptr = /* whatever */;
std::unique_ptr<T> up = ptr;

You have to change the second line to this:

std::unique_ptr<T> up (ptr);

The original version is (basically) implicitly turning the assignment into:

std::unique_ptr<T> up (std::unique_ptr<T>(ptr));

The copy constructor has been deleted. "deleted function" is C++0x-speak for explicitly removing an implicit special member function. In this case, the copy constructor.

like image 44
Steve M Avatar answered Jan 31 '23 10:01

Steve M