Passing function type as default template parameter

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
JonnyG
Messages
233
Reaction score
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:

[CODE lang="cpp" title="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[/CODE]

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.
 
Physics news on Phys.org
JonnyG said:
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:

[CODE lang="cpp" title="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[/CODE]

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.
Non-static member functions are bound to individual instantiations of the class through a hidden this parameter. So you have to do it a little different than if it was a stand alone function. I think this should work.
C:
deleter( std::bind(&single_ptr::del, this) )
https://en.cppreference.com/w/cpp/utility/functional/bind
 
Reply
  • Like
Likes   Reactions: JonnyG