我的make_unique有什么问题,如何实现以适应用例

时间:2018-09-20 05:07:41

标签: c++ memory-management smart-pointers unique-ptr

我已经在下面的代码中编写了唯一指针的基本版本。我已经复制了Herb Sutter的make_unique()的实现,但无法使其正常工作。我遇到错误:

更新: 我已删除using namespace std;

D:\coding\cpp\smartPointers>g++ -std=c++11 uniquePtrImpl.cpp

uniquePtrImpl.cpp:33:41: error: expansion pattern 'Args&&' contains no argument packs

  UniquePointer make_unique(Args&&... args) {


uniquePtrImpl.cpp: In function 'UniquePointer make_unique()':

uniquePtrImpl.cpp:34:52: error: 'args' was not declared in this scope

   return UniquePointer(new T(std::forward(args) ...));


uniquePtrImpl.cpp:34:58: error: expansion pattern 'forward()' contains no argument packs

   return UniquePointer(new T(std::forward(args) ...));

uniquePtrImpl.cpp: In function 'int main()':

uniquePtrImpl.cpp:48:66: error: no matching function for call to 
'make_unique(const char [6], int)'

   UniquePointer aPerson = make_unique("Sweha", 25);


uniquePtrImpl.cpp:33:19: note: candidate: template UniquePointer make_unique()

  UniquePointer make_unique(Args&&... args) {


uniquePtrImpl.cpp:33:19: note:   template argument deduction/substitution failed:


uniquePtrImpl.cpp:48:66: note:   candidate expects 0 arguments, 2 provided
   UniquePointer aPerson = make_unique("Sweha", 25);
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

struct Person {
    string name;
    int age;
    Person(string const& name, int const& age): name{name}, age{age} { }
    void show() {
        cout << "Person's name is : " << name << " and age is : " << age << endl;
    }
};

template <typename T>
class UniquePointer {
private:
    T *ptr;
public:
    explicit UniquePointer(T* p = nullptr): ptr{p} {}
    UniquePointer(UniquePointer const& up) = delete;
    UniquePointer& operator=(UniquePointer const& up) = delete;

    ~UniquePointer() { delete ptr; }

    T& operator*() { return *ptr; }
    T* operator->() { return ptr; }
};

template <typename T, typename Args>
UniquePointer<T> make_unique(Args&&... args) {
    return UniquePointer<T>(new T(std::forward<Args>(args) ...));
}

int main() {
    UniquePointer<string> sp(new string("Hello"));
    cout << *sp << endl;

    *sp = "Wassup";
    cout << *sp << endl;

    UniquePointer<Person> sPerson(new Person("Tousif", 28));

    sPerson->show();

    UniquePointer<Person> aPerson = make_unique<Person>("Sweha", 25);
    aPerson->show();

    return 0;
} 

已解决:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

struct Person {
    string name;
    int age;
    Person(string const& name, int const& age): name{name}, age{age} { }
    void show() {
        cout << "Person's name is : " << name << " and age is : " << age << endl;
    }
};

template <typename T>
class UniquePointer {
private:
    T *ptr;
public:
    explicit UniquePointer(T* p = nullptr): ptr{p} {}

    UniquePointer(UniquePointer const& up) = delete;
    UniquePointer& operator=(UniquePointer const& up) = delete

    UniquePointer(UniquePointer&& up) {
        ptr = std::move(up.ptr);
    }

    UniquePointer& operator=(UniquePointer&& up){
        ptr = std::move(up.ptr);
        return *this;
    }

    ~UniquePointer() { delete ptr; }

    T& operator*() { return *ptr; }
    T* operator->() { return ptr; }
};

template <typename T, typename...   Args>
UniquePointer<T> make_unique(Args&&... args) {
    return UniquePointer<T>(new T(std::forward<Args>(args) ...));
}

int main() {
    UniquePointer<string> sp(new string("Hello"));
    cout << *sp << endl;

    *sp = "Wassup";
    cout << *sp << endl;

    UniquePointer<Person> sPerson(new Person("Tousif", 28));

    sPerson->show();

    UniquePointer<Person> aPerson = make_unique<Person>("Sweha", 25);
    aPerson->show();

    return 0;
} 

1 个答案:

答案 0 :(得分:3)

template <typename T, typename Args> // <------
UniquePointer<T> make_unique(Args&&... args) {
    return UniquePointer<T>(new T(std::forward<Args>(args) ...));
}

Args不是参数包。 更改为:

template <typename T, typename ... Args> // <------

但是您的代码无法编译,因为移动操作已删除(如何从UniquePointer中获得make_unique,而没有这些操作?)。这些默认情况下已删除,因为您已定义了复制操作。 添加

 UniquePointer(UniquePointer&&) = default;
 UniquePointer& operator=(UniquePointer&&) = default;