function pointer to functor - c++

I have a static function foo but the API I want to call only accept pointer to a functor (of similar interface ). Is there a way to pass foo to the API? or I need to re-implement foo in terms of functor.
Example code:
template<typename ReturnType, typename ArgT>
struct Functor: public std::unary_function<ArgT,ReturnType>
{
virtual ~Functor () {}
virtual ReturnType operator()( ArgT) = 0;
};
// I have a pre written function
static int foo (int a) {
return ++a;
}
// I am not allowed to change the signature of this function :(
static void API ( Functor<int,int> * functor ) {
cout << (*functor) (5);
}
int main (void) {
API ( ??? make use of `foo` somehow ??? );
return 0;
}
My question is for calling API, implementing Functor is only solution or there is a way by which I could use foo to pass it to API?
Will boost::bind help here?
I mean boost::bind(foo, _1) will make function object out of foo and then if there is a way to form desired functor out of function object?

It seems like you have no option other than writing your own functor as a derived type of Functor<int, int>. However, you could save yourself some trouble by providing an intermediate class template functor that can be instantiated from a functor or funciton pointer:
template<typename R, typename A>
struct GenericFunctor<R, A> : public Functor<R, A>
{
template <typename F>
MyFunctor(F f) : f_(f) {}
ReturnType operator()(A arg) = { return f_(arg);}
private:
std::function<R(A)> f_; // or boost::function
};
Then you can say
GenericFunctor<int, int> fun = foo;
API(&fun); // works. GenericFinctor<int,int> is a Functor<int,int>
This is just a workaround for the fact that the stuff you have been given is so awful.

Related

How to initiate std::unique_ptr in the constructor with std::make_unique() [duplicate]

I have a class with a unique_ptr member.
class Foo {
private:
std::unique_ptr<Bar> bar;
...
};
The Bar is a third party class that has a create() function and a destroy() function.
If I wanted to use a std::unique_ptr with it in a stand alone function I could do:
void foo() {
std::unique_ptr<Bar, void(*)(Bar*)> bar(create(), [](Bar* b){ destroy(b); });
...
}
Is there a way to do this with std::unique_ptr as a member of a class?
Assuming that create and destroy are free functions (which seems to be the case from the OP's code snippet) with the following signatures:
Bar* create();
void destroy(Bar*);
You can write your class Foo like this
class Foo {
std::unique_ptr<Bar, void(*)(Bar*)> ptr_;
// ...
public:
Foo() : ptr_(create(), destroy) { /* ... */ }
// ...
};
Notice that you don't need to write any lambda or custom deleter here because destroy is already a deleter.
It's possible to do this cleanly using a lambda in C++11 (tested in G++ 4.8.2).
Given this reusable typedef:
template<typename T>
using deleted_unique_ptr = std::unique_ptr<T,std::function<void(T*)>>;
You can write:
deleted_unique_ptr<Foo> foo(new Foo(), [](Foo* f) { customdeleter(f); });
For example, with a FILE*:
deleted_unique_ptr<FILE> file(
fopen("file.txt", "r"),
[](FILE* f) { fclose(f); });
With this you get the benefits of exception-safe cleanup using RAII, without needing try/catch noise.
You just need to create a deleter class:
struct BarDeleter {
void operator()(Bar* b) { destroy(b); }
};
and provide it as the template argument of unique_ptr. You'll still have to initialize the unique_ptr in your constructors:
class Foo {
public:
Foo() : bar(create()), ... { ... }
private:
std::unique_ptr<Bar, BarDeleter> bar;
...
};
As far as I know, all the popular c++ libraries implement this correctly; since BarDeleter doesn't actually have any state, it does not need to occupy any space in the unique_ptr.
Unless you need to be able to change the deleter at runtime, I would strongly recommend using a custom deleter type. For example, if use a function pointer for your deleter, sizeof(unique_ptr<T, fptr>) == 2 * sizeof(T*). In other words, half of the bytes of the unique_ptr object are wasted.
Writing a custom deleter to wrap every function is a bother, though. Thankfully, we can write a type templated on the function:
Since C++17:
template <auto fn>
struct deleter_from_fn {
template <typename T>
constexpr void operator()(T* arg) const {
fn(arg);
}
};
template <typename T, auto fn>
using my_unique_ptr = std::unique_ptr<T, deleter_from_fn<fn>>;
// usage:
my_unique_ptr<Bar, destroy> p{create()};
Prior to C++17:
template <typename D, D fn>
struct deleter_from_fn {
template <typename T>
constexpr void operator()(T* arg) const {
fn(arg);
}
};
template <typename T, typename D, D fn>
using my_unique_ptr = std::unique_ptr<T, deleter_from_fn<D, fn>>;
// usage:
my_unique_ptr<Bar, decltype(&destroy), destroy> p{create()};
You know, using a custom deleter isn't the best way to go, as you will have to mention it all over your code.
Instead, as you are allowed to add specializations to namespace-level classes in ::std as long as custom types are involved and you respect the semantics, do that:
Specialize std::default_delete:
template <>
struct ::std::default_delete<Bar> {
default_delete() = default;
template <class U>
constexpr default_delete(default_delete<U>) noexcept {}
void operator()(Bar* p) const noexcept { destroy(p); }
};
And maybe also do std::make_unique():
template <>
inline ::std::unique_ptr<Bar> ::std::make_unique<Bar>() {
auto p = create();
if (!p)
throw std::runtime_error("Could not `create()` a new `Bar`.");
return { p };
}
You can simply use std::bind with a your destroy function.
std::unique_ptr<Bar, std::function<void(Bar*)>> bar(create(), std::bind(&destroy,
std::placeholders::_1));
But of course you can also use a lambda.
std::unique_ptr<Bar, std::function<void(Bar*)>> ptr(create(), [](Bar* b){ destroy(b);});
#include "fmt/core.h"
#include <memory>
class example {};
void delete_example(example *)
{
fmt::print("delete_example\n");
}
using example_handle = std::unique_ptr<example, decltype([] (example * p)
{
delete_example(p);
})>;
int main()
{
example_handle handle(new example);
}
Just my two cents, using C++20.
https://godbolt.org/z/Pe3PT49h4
With a lambda you can get the same size as a plain std::unique_ptr. Compare the sizes:
plain: 8
lambda: 8
fpointer: 16
std::function: 40
Which is the output of the following. (I declared the lambda outside the scope of the class. Not sure if you can scope it inside the class.)
#include <iostream>
#include <memory>
#include <functional>
struct Bar {};
void destroy(Bar* b) {}
Bar* create() { return 0; }
auto lambda_destroyer = [](Bar* b) {destroy(b);};
class Foo {
std::unique_ptr<Bar, decltype(lambda_destroyer)> ptr_;
public:
Foo() : ptr_(create(), lambda_destroyer) { /* ... */ }
};
int main()
{
std::cout << "plain: " << sizeof (std::unique_ptr<Bar>) << std::endl
<< "lambda: " << sizeof (std::unique_ptr<Bar, decltype(lambda_destroyer)>) << std::endl
<< "fpointer: " << sizeof (std::unique_ptr<Bar, void(*)(Bar*)>) << std::endl
<< "std::function: " << sizeof (std::unique_ptr<Bar, std::function<void(Bar*)>>) << std::endl;
}
I'm fairly convinced that this is the best current way to do it:
#include <memory>
#include <stdio.h>
template <typename T, auto fn>
struct Deleter
{
void operator()(T *ptr)
{
fn(ptr);
}
};
template <typename T, auto fn>
using handle = std::unique_ptr<T, Deleter<T, fn>>;
using file = handle<FILE, fclose>;
int main()
{
file f{fopen("a.txt", "w")};
return 0;
}
Because you've specified a Functor as the deleter in the unique_ptr's template arguments, you don't need to set a deleter when calling its constructor.
The Deleter functor uses "template auto" to take a deletion function (in this example: fclose) as a template argument, so this needs C++17.
Expanding it to support other types is just one extra "using" line per type.
Simple is also:
class Foo {};
class Bar
{
public:
Bar()
{
// actual initialisation at some point
}
private:
std::unique_ptr<Foo, void(*)(Foo*)> foo = {{}, {}}; // or = {nullptr, {}}
};
Sure, you can also create some helper function to do the job to not have the initial state at any time.
In fact, in your specific scenario, the cleanest way is to actually put your Bar (not mine, sorry for the confusion) into a simple wrapper class, which makes reuse easier.

std::vector of movable-only lambdas, is it possible?

I'd like to have a collection of lambdas, with the requirement that the lambas must not be copied, only moved.
This is because the lambas may need to move-capture some of their arguments that are not copy-constructible.
Example:
NonCopyableType varName ;
auto func = [a=move(varName)](){ ... } ; //varName is move-captured
After this I want to store func in a vector, but I can't use the std::function type because it requires the lambdas to be copyable.
vector<function<void()>> list ;
list.push_back(func) ; //won't work
Is it possible to do this some other way?
Sure. Just write your own function clone that is move-only. Here's a simplified version that only supports nullary callables, but you can see how it can be extended:
class move_function
{
struct placeholder {
virtual ~placeholder() = default;
virtual void call() = 0;
};
template <class T>
struct holder : placeholder {
T f;
void call() override { f(); }
};
std::unique_ptr<placeholder> f_;
public:
template <class F,
class R = std::result_of_t<F&()>,
std::enable_if_t<!std::convertible<std::decay_t<F>*, move_function*>::value, int> = 0>
move_function(F&& f)
: f_(new std::decay_t<F>{std::forward<F>(f)})
{ }
void operator()() const { f_->call(); }
};
All the implicitly defined special member functions already do the right thing for us.

Any way to let the use of function pointers as template arguments easier?

I can use function pointer as template argument as the following
template<class R, class T, R (*Fun)(T)>
class MyClass;
Any way to make it is easy as
MyClass<&MyFun> a;
Here is a horrible answer, but I cannot think of a better one.
template<typename R, typename Arg, R(*Fun)(Arg)>
class MyClass {};
template<typename R, typename Arg>
struct MyClassHelper {
template<R(*Fun)(Arg)>
struct Class {
typedef MyClass<R, Arg, Fun> type;
};
};
template<typename R, typename Arg>
MyClassHelper<R, Arg> GetMyClass(R(*Fun)(Arg)); // no impl
void MyFun(int) {}
int main() {
typedef decltype( GetMyClass(&MyFun) ) A;
typedef A::Class<&MyFun> B;
typedef B::type a;
// or, in one line:
decltype( GetMyClass(&MyFun) )::Class<&MyFun>::type b;
}
which is ugly as sin. But at least it extracts the argument types of MyFun without repeating them...
It's not possible exactly as in the question, but yes it's possible if you flex your design a little bit. Let's take an example:
Suppose you have below function:
int foo (int i) { return i; }
Now you want to write:
MyClass<&foo> a; // instead of `Myclass<int, int, &foo> a;
Here how you will achieve it. First change the simple function:
int foo (int i) { return i; }
to encapsulated function object:
struct foo { // <--- function name here
int operator () (int i) { return i; } // <--- function body here
};
Both are almost same (in the case of function object an extra this pointer is passed which doesn't happen in the free function case):
int x = foo(2); // 1st case
int x = foo_(2); // 2nd case where `foo_` is an object of `struct foo`
Now you can use simply as you want!
template<class Func>
class MyClass {...}
MyClass<foo> a;
Here is a working demo.
There's already std::ptr_fun. With C++11, you can use it as
auto a = std::ptr_fun(&MyFun);
Update:
As the other attempts, and non attempts BTW, have shown, isn't it possible with your kind of template, least of all "as easy as" ;-). What you can do however, is sort of reimplement the existing standard template function pattern (std::ptr_fun, std::make_pair and the like) to return the desired type, if your main goal is "as easy as".

how to deduce the return type of a function in template

I am trying to write a template class named Binder that bind functions and parameters as whole, distinguished by the returning type of the binded function, this is my approach:
template <typename return_type>
class Binder
{
public:
virtual return_type call() {}
};
invoking call will invoke some pre-binded functions with parameters, and return the result.
I want some template classes inherited from Binder that do the real binding job.
below is a one-parameter-function binding class:
template<typename func_t, typename param0_t>
class Binder_1 : public Binder< ***return_type*** >
// HOW TO DETERMINE THE RETURN TYPE OF func_t?
// decltype(func(param0)) is available when writing call(),
// but at this point, I can't use the variables...
{
public:
const func_t &func;
const param0_t &param0;
Binder_1 (const func_t &func, const param0_t &param0)
: func(func), param0(param0) {}
decltype(func(param0)) call()
{
return func(param0);
}
}
// Binder_2, Binder_3, ....
This is what I want to achieve:
template<typename func_t, typename param0_t>
Binder_1<func_t, param0_t> bind(const func_t &func, const param0_t &param0)
{
reurn Binder_1<func_t, param0_t>(func, param0);
}
// ... `bind` for 2, 3, 4, .... number of paramters
int func(int t) { return t; }
double foo2(double a, double b) { return a > b ? a : b; }
double foo1(double a) { return a; }
int main()
{
Binder<int> int_binder = bind(func, 1);
int result = int_binder.call(); // this actually calls func(1);
Binder<double> double_binder = bind(foo2, 1.0, 2.0);
double tmp = double_binder.call(); // calls foo2(1.0, 2.0);
double_binder = bind(foo1, 1.0);
tmp = double_binder.call(); // calls foo1(1.0)
}
can bind function in boost library be adapted to achieve this functionality?
similar solutions are welcome too!
Introducing std::declval<T>().
This is a dummy function declared as:
template <typename T>
typename std::add_rvalue_reference<T>::type declval();
// This means it returns T&& if T is no a reference
// or T& if T is already a reference
and never actually defined.
It is therefore only to be used within unevaluated contexts such as sizeof or... decltype!
With this, you get:
template<typename func_t, typename param0_t>
class Binder_1: public Binder<decltype(std::declval<func_t>()(std::declval<param0_t>())>
It is a bit verbose, but hey! It works :)
You might be able to use result_of.

multiple specializations of a variadic template with a statically bound member function pointer?

Is it possible to have multiple specializations of a variadic template where one of the template parameters is a statically bound member function pointer?
I'm attempting to build a delegate where the callback function is a compile time constant - thereby aiding the optimizer to see past the function pointer boundary.
I have the following code where I pass a member function pointer as a template parameter, and since the function pointer is a constant which is known at compile-time, my expectation is that the optimizer will be able to work through the function pointer boundary.
I have created 2 delegates, delegate0 and delegate1, which are for member functions which have 0 and 1 arguments respectively.
#include <iostream>
template<class class_t, void (class_t::*mem_func_t)()>
struct delegate0
{
delegate0( class_t *obj_ )
: _obj(obj_)
{ }
void operator()()
{
(_obj->*mem_func_t)();
}
private:
class_t *_obj;
};
template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate1
{
delegate1( class_t *obj_, arg0 a0_ )
: _obj(obj_)
, _a0(a0_)
{ }
void operator()()
{
(_obj->*mem_func_t)(_a0);
}
private:
class_t *_obj;
arg0 _a0;
};
struct app
{
void cb()
{
std::cout << "hello world\n";
}
void cb1(int i)
{
std::cout << "hello world " << i << "\n";
}
};
int main()
{
app* foo = new app;
delegate0<app, &app::cb> f(foo);
f();
delegate1<app, int, &app::cb1> f1(foo, 5);
f1();
}
However, I would like to improve on this in 2 ways:
All permutations of the number of arguments to be specializations of a variadic delegate template.
Use template argument deduction such that declaring something like delegate<&app::cb> (when cb is not ambiguous), class_t, mem_func_t, arg0, arg1, etc... are all deduced from the signature for app::cb.
I realize that a member function pointer is not a type, but just like you can pass a particular integer as a template parameter (ala template recursion used in metaprogramming), I figure you can have a specific member function pointer as a parameter - thereby allowing static binding to that function.
Is what I'm after even possible?
If not, is either of 1 or 2 above possible?
I would really appreciate a working example, because I've been banging my head against my keyboard with no success as of yet.
I have the following miserable attempt. It is clearly not what I'm looking for, but in order to show the direction I've been heading, I thought it perhaps useful to include.
template<typename...>
struct delegate;
template<class class_t, void (class_t::*mem_func_t)()>
struct delegate<class_t, decltype(mem_func_t)>
{
delegate( class_t *obj_ )
: _obj(obj_)
{ }
void operator()(mem_func_t f)
{
(_obj->*f)();
}
class_t *_obj;
};
template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate<class_t, arg0, decltype(mem_func_t)>
{
delegate( class_t *obj_, arg0 a0_ )
: _obj(obj_)
, _a0(a0_)
{ }
void operator()()
{
(_obj->*mem_func_t)(_a0);
}
class_t *_obj;
arg0 _a0;
};
Declare a template taking any types:
template <typename T, T value>
struct Delegate;
and then specialize it for member function objects (do it 4 times for each cv-qualifier):
template <typename R, typename C, typename... A, R (C::* value)(A...) const>
struct Delegate<R(C::*)(A...) const, value>
{
// do whatever you like with R, C, A... .
};
As I've answered before, you'll need decltype:
Delegate<decltype(&SomeClass::method), &SomeClass::method> del;
Alternatively, you could use my function_traits class which can extract the R, C and A... from T directly so you don't need to specialize, but decltype and repeating the method is still needed.