Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define typedef of function pointer which has template arguments

Tags:

I would like to make typedef for function pointer which has stl container as argument and this container has unknown type. Something like this:

typedef void (* TouchCallBack)(GLRenderer*, const MotionEvent&, std::vector<T >);

it's possible? (especially in c++ 03)

like image 910
strobe Avatar asked Feb 13 '13 08:02

strobe


People also ask

What is typedef in function pointer?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.

What are template arguments?

In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is typedef void?

typedef void (*MCB)(void); This is one of the areas where there is a significant difference between C, which does not - yet - require all functions to be prototyped before being defined or used, and C++, which does.

What is function pointer How do you declare it give suitable example?

Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.


1 Answers

I don't know of any C++03 solution exactly like that, and it's not built into the language, but in C++11, this is possible with using aliases:

template<typename T>
using TouchCallBack = void (*)(GLRenderer*, const MotionEvent&, std::vector<T >);

One workaround for C++03 is using a struct:

template<typename T>
struct TouchCallBack {
    typedef void (*type)(GLRenderer*, const MotionEvent&, std::vector<T >);
};

//use like TouchCallBack<int>::type
like image 55
chris Avatar answered Oct 07 '22 18:10

chris