A
A
Anton Kashcheev2014-06-25 04:47:29
C++ / C#
Anton Kashcheev, 2014-06-25 04:47:29

Is it possible to pass a function pointer as a template parameter?

Recently I started to deal with templates, and this question immediately arose.
With non-static class methods, everything is clear, it turned out to be easier than I thought:

template<class Obj, void*(Obj::*publicMethod)(void*))>
func(Obj* pObj, void* arg)
{
    return pObj->publicMethod(arg);
}

And how can something similar be "cranked" with ordinary functions? And is it possible?
Thank you all in advance for your response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AxisPod, 2014-06-25
@kachsheev

Using typedef it is possible.
ideone.com/tzKAng
But don't do that :) In this case, each forwarded function will generate its own code. Secondly, there’s no way to throw std:: function, nothing will most likely work with lambdas either, I’m generally silent about std::bind.

D
DancingOnWater, 2014-06-25
@DancingOnWater

Em.
std::function
std::bind
Lambda functions
Classic function pointers.
Study to enlightenment

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question