Я написал базовую версию уникального указателя в коде ниже.Я скопировал реализацию Херба Саттера 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;
}