Passing function type as default template parameter

Click For Summary
SUMMARY

The discussion focuses on creating a custom smart pointer template named `single_ptr` in C++. The template accepts two parameters: `T`, the type of the raw pointer, and `D`, which defaults to `std::function`. The user encounters a compilation error when trying to instantiate `single_ptr` due to an unresolved function type for the deleter. The solution involves using `std::bind` to correctly bind the member function `del` to the instance of `single_ptr`.

PREREQUISITES
  • C++11 or later for using `std::function` and `std::bind`.
  • Understanding of templates in C++.
  • Familiarity with smart pointers and memory management concepts.
  • Knowledge of function binding in C++.
NEXT STEPS
  • Learn about C++ smart pointers, specifically `std::unique_ptr` and `std::shared_ptr`.
  • Explore the use of `std::bind` and its applications in C++.
  • Investigate advanced template programming techniques in C++.
  • Study error handling and debugging techniques for template instantiation errors in C++.
USEFUL FOR

C++ developers, software engineers working with memory management, and anyone interested in implementing custom smart pointers in their applications.

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
 
  • Like
Likes   Reactions: JonnyG

Similar threads

Replies
63
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
4K