Passing function type as default template parameter

AI Thread Summary
The discussion revolves around creating a custom smart pointer template called `single_ptr`, which accepts two template parameters: T for the type of the raw pointer and D for a custom deleter function type. The user encounters a compilation error when trying to instantiate `single_ptr<int>`, specifically related to initializing the `deleter` member with a non-static member function. The suggested solution involves using `std::bind` to correctly bind the member function `del` to the current instance of `single_ptr`. This approach ensures that the deleter is properly set up to delete the managed pointer when the smart pointer goes out of scope. The conversation highlights the intricacies of handling function types and member functions in C++.
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
 
Back
Top