Passing function type as default template parameter

In summary, a function type in C++ refers to the type of a function, including the return type and parameter types. A default template parameter is a value or type that is automatically assigned to a template parameter if no explicit value is provided. To pass a function type as a default template parameter, it can be specified as the default value for the template parameter. This allows for more flexibility and customization when using templates, as well as code reusability. However, there are limitations such as the function type needing to be known at compile time and matching the template parameter exactly.
  • #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:

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.
 
Physics news on Phys.org
  • #2
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:

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.
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 JonnyG

FAQ: Passing function type as default template parameter

What is a function type?

A function type is a type that represents a function in programming. It includes the return type, parameter types, and any qualifiers or exception specifications.

What does it mean to pass a function type as a default template parameter?

Passing a function type as a default template parameter means that the function type is used as the default value for a template parameter. This allows the template to work with different types of functions without needing to explicitly specify the function type each time.

Why would you want to use a function type as a default template parameter?

Using a function type as a default template parameter allows for more flexibility and reusability in templates. It also simplifies the code by eliminating the need to specify the function type every time the template is used.

Can you give an example of passing a function type as a default template parameter?

Yes, an example would be a template for sorting a vector of elements using a custom comparison function. The function type for the comparison function can be passed as a default template parameter, allowing the template to work with different types of comparison functions.

Are there any limitations or considerations when passing a function type as a default template parameter?

Yes, there are a few limitations and considerations to keep in mind when passing a function type as a default template parameter. For example, the function type must be callable and have the same signature as the default argument. Additionally, the function type cannot be a member function or a function with a variable number of arguments.

Similar threads

Back
Top