C ++ x0 unique_ptr GCC 4.4.4 [英] C++x0 unique_ptr GCC 4.4.4

查看:147
本文介绍了C ++ x0 unique_ptr GCC 4.4.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用C ++ x0的unique_ptr,通过执行

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

#include <memory> 

并与-std = c ++ 0x进行合并,但是它会抛出很多错误示例。

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:

如果我的类SomeType有一个方法从地图返回一个元素(使用键)说

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?

推荐答案

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

您不能将一个左值传递给接受 unique_pointer ,因为 unique_pointer 没有复制构造函数。您必须移动左值(将其转换为xvalue)或传递prvalue:

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"));

添加方法中,一点复杂。首先,你必须从 ot 移动,因为形式参数总是左值(因为它们有名字)。第二,你不能从 ot 移动,并获得 ot-> name()作为 mappair 函数,因为参数求值的顺序在C ++中未指定。因此,我们必须在单独的语句中从 ot 之前获取 ot-> name()

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))); 
}

希望这有帮助。注意,在没有(sane)情况下,两个 unique_ptr 对象指向同一个东西。如果您需要该功能, unique_ptr 不是您想要的。

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.

这篇关于C ++ x0 unique_ptr GCC 4.4.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆