c++ deducing a function pointer type

Is it possible to deduce a non-type function pointer type template argument (function pointer) from a function argument?

template <void(*fptr)()>
  void test(void(*fp)()) { fp(); }

to call this function I must explicitly declare the function template parameter:

test<somefunc>(somefunc);

I know I could also do it this way:

template <void(*fptr)()>
  void test() { fp(); }

test<somefunc>();

But I am just wondering if It is possible to do this way:

template <void(*fptr)()>
  void test() { fp(); }

test(somefunc);

Is it possible to declare in such a way that the compilier (GCC 4.7) will deduce from the function arguments?

Thanks alot in advance, been really wondering how to do this. -Bryan

Answers


Bryan, that seems to be quite eccentric mix of low-level C and C++. Why do you need that? Why not to use functors?

struct clean
{
    void operator() () 
    {
        // do something here        
    }
};

template <typename FuncType> void call_func(FuncType func)
{
    func();
}

// here is how to pass 'clean' to be called
call_func(clean());

More on functors, for example, here: http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html


Is it possible to deduce a non-type template argument (function pointer) from a function argument?

No. Function arguments are runtime entities, and template arguments are compile-time entities. To be deduced, such a template argument would have to be deduced at runtime, which is just not possible.


I this this may do what you want:

Declare a base template which doesn't have a function type yet:

template <typename T> void test(T fp) { printf("function signature not supported\n"); }

Specialise for function types (mostly number of arguments):

typedef void(fptr0)();
template <> void test(fptr0 fp) { fp(); }
typedef void(fptr1)(int);
template <> void test(fptr1 fp) { fp(1); }

Declare some test functions with different signatures:

void myfn0() { printf("hi 0\n"); }
void myfn1(int x) { printf("hi 1:%i\n",x); }
void myfnD(float y) { printf("hi D %f\n",y); }

Now execute them:

int main(int,char**) {
   test(myfn0);
   test(myfn1);
   test(myfnD);
   return 0;
}

Result:

hi 0
hi 1:1
function signature not supported

Need Your Help

Using the solution's configuration name as a test condition for MSBuild Target

visual-studio-2010 msbuild wix target

I finally realized why my BeforeBuild Target is no longer executing as expected -- it's so silly, it's because the project's configuration names had changed. But what I really want to do is test f...

Prevent duplicate inserts without using unique keys in Oracle

java jdbc database-concurrency

This is a use case in member enrollment via web application/web service. We have a complex algorithm for checking if a member is duplicate, by looking at multiple tables like phone,address etc. The

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.