How to get rid of the manual class template parameter specification

Viewed 154

Is there any more generic way to write a Finalizer class than this?

#include <functional>
#include <iostream>

template <typename T>
class Finalizer
{
public:
    Finalizer(const std::function<T>& f) : _f(f) {}

    ~Finalizer()
    {
        _f();
    }

private:
    std::function<T> _f;
};

int main()
{
    Finalizer<void()> finalizer([]() { std::cout << "str" << std::endl; });
}

I want to get rid of the manual class template parameter specification to be able to write code like this:

Finalizer finalizer([]() { std::cout << "str" << std::endl; });

Is it possible?

1 Answers
Related