相关疑难解决方法(0)

unique_ptr提升等价?

在boost库中是否有一些与C++ 1x的std :: unique_ptr等效的类?我正在寻找的行为是能够拥有异常安全的工厂功能,就像这样......

std::unique_ptr<Base> create_base()
{
    return std::unique_ptr<Base>(new Derived);
}

void some_other_function()
{
    std::unique_ptr<Base> b = create_base();

    // Do some stuff with b that may or may not throw an exception...

    // Now b is destructed automagically.
}
Run Code Online (Sandbox Code Playgroud)

编辑:现在,我正在使用这个黑客,这似乎是我能在这一点上得到的最好的......

Base* create_base()
{
    return new Derived;
}

void some_other_function()
{
    boost::scoped_ptr<Base> b = create_base();

    // Do some stuff with b that may or may not throw an exception...

    // Now b is deleted automagically.
}
Run Code Online (Sandbox Code Playgroud)

c++ boost unique-ptr c++11

55
推荐指数
4
解决办法
4万
查看次数

标签 统计

boost ×1

c++ ×1

c++11 ×1

unique-ptr ×1