- #1
JonnyG
- 234
- 45
The book is asking me to write my own unique_ptr template (after just covering a bit about templates). I called my template single_ptr, and I gave it two template parameters, T and D. T is supposed to be the type that the raw pointer points to. D is supposed to represent a function type so that the user of the class can pass their own deleter if need be. But I also want my own default deleter. This is what I have:
When I try to create a single_ptr<int> object in in my main.cpp file to test it out, I get:
"error: no matching function to call to 'std::function<void()>::function(<unresolved overloaded function type>)"
I can't figure out why this isn't working.
single_ptr.hpp:
#ifndef SINGLE_PTR_HPP
#define SINGLE_PTR_HPP
#include <functional>
using std::function;
template <typename T, typename D = function<void()>> class single_ptr {
public:
single_ptr() : ptr(nullptr), deleter(del) { } //THIS IS WHERE THE ERROR OCCURS
single_ptr(T *p) : ptr(p) { }
single_ptr(const single_ptr &sp) = delete;
single_ptr(single_ptr &&sp);
~single_ptr() { deleter; }
T* get() { return ptr; }
private:
T *ptr;
D deleter;
void del() { delete ptr; }
};
#endif
When I try to create a single_ptr<int> object in in my main.cpp file to test it out, I get:
"error: no matching function to call to 'std::function<void()>::function(<unresolved overloaded function type>)"
I can't figure out why this isn't working.