相关疑难解决方法(0)

为什么这个RAII move-only类型没有正确模拟`std :: unique_ptr`?

我从这个问题中获取代码并编辑它以通过显式调用其中一个移动构造对象的析构函数来生成段错误:

using namespace std;

struct Foo
{
    Foo()  
    {
        s = new char[100]; 
        cout << "Constructor called!" << endl;  
    }

    Foo(const Foo& f) = delete;

    Foo(Foo&& f) :
      s{f.s}
    {
        cout << "Move ctor called!" << endl;   
        f.s = nullptr;
    }

    ~Foo() 
    { 
        cout << "Destructor called!" << endl;   
        cout << "s null? " << (s == nullptr) << endl;
        delete[] s; // okay if s is NULL
    }

    char* s;
};

void work(Foo&& f2)
{
    cout << …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr move-semantics c++11

0
推荐指数
1
解决办法
180
查看次数

标签 统计

c++ ×1

c++11 ×1

move-semantics ×1

unique-ptr ×1