function-pointers

How do I get the argument types of a function pointer in a variadic template class?

萝らか妹 提交于 2019-12-17 05:51:09
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

How do I get the argument types of a function pointer in a variadic template class?

别来无恙 提交于 2019-12-17 05:51:02
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

How to make a function return a pointer to a function? (C++)

為{幸葍}努か 提交于 2019-12-17 05:46:05
问题 I'm trying to make a function that takes a character, then returns a pointer to a function depending on what the character was. I just am not sure how to make a function return a pointer to a function. 回答1: int f(char) { return 0; } int (*return_f())(char) { return f; } No, seriously, use a typedef :) 回答2: #include <iostream> using namespace std; int f1() { return 1; } int f2() { return 2; } typedef int (*fptr)(); fptr f( char c ) { if ( c == '1' ) { return f1; } else { return f2; } } int

How define an array of function pointers in C

天涯浪子 提交于 2019-12-17 05:37:31
问题 I've a little question. I'm trying to define an array of function pointers dynamically with calloc . But I don't know how to write the syntax. Thanks a lot. 回答1: The type of a function pointer is just like the function declaration, but with "(*)" in place of the function name. So a pointer to: int foo( int ) would be: int (*)( int ) In order to name an instance of this type, put the name inside (*), after the star, so: int (*foo_ptr)( int ) declares a variable called foo_ptr that points to a

What is guaranteed about the size of a function pointer?

我的梦境 提交于 2019-12-17 04:32:20
问题 In C, I need to know the size of a struct, which has function pointers in it. Can I be guaranteed that on all platforms and architectures: the size of a void* is the same size as a function pointer? the size of the function pointer does not differ due to its return type? the size of the function pointer does not differ due to its parameter types? I assume the answer is yes to all of these, but I want to be sure. For context, I'm calling sizeof(struct mystruct) and nothing more. 回答1: From C99

How to get function's name from function's pointer in Linux kernel?

非 Y 不嫁゛ 提交于 2019-12-17 03:54:13
问题 How to get function's name from function's pointer in C? Edit: The real case is: I'm writing a linux kernel module and I'm calling kernel functions. Some of these functions are pointers and I want to inspect the code of that function in the kernel source. But I don't know which function it is pointing to. I thought it could be done because, when the system fails (kernel panic) it prints out in the screen the current callstack with function's names. But, I guess I was wrong... am I? 回答1: That

Python function pointer

主宰稳场 提交于 2019-12-17 03:01:53
问题 I have a function name stored in a variable like this: myvar = 'mypackage.mymodule.myfunction' and I now want to call myfunction like this myvar(parameter1, parameter2) What's the easiest way to achieve this? 回答1: funcdict = { 'mypackage.mymodule.myfunction': mypackage.mymodule.myfunction, .... } funcdict[myvar](parameter1, parameter2) 回答2: It's much nicer to be able to just store the function itself, since they're first-class objects in python. import mypackage myfunc = mypackage.mymodule

Callback functions in Java

ぐ巨炮叔叔 提交于 2019-12-17 02:39:07
问题 Is there a way to pass a call back function in a Java method? The behavior I'm trying to mimic is a .Net Delegate being passed to a function. I've seen people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things. 回答1: If you mean somthing like .NET anonymous delegate, I think Java's anonymous class can be used as well. public class Main { public interface Visitor{ int doJob(int a, int b); } public static void

template function pointer parameter in a class method

限于喜欢 提交于 2019-12-14 03:04:38
问题 I have a class method which takes in a function pointer of an object of any class but with a specific return and parameter list. The problem is that the compiler returns that what I pass is not the same as the arguments of the method I'm using. There are other things I'm have trouble with but I will point them out below and as always not every detail is here only whatever is important. Class.h : template<typename Value, typename ...Args> class thing <Value(Args...)> { /* moar code */ template

comp.lang.c faq: Function that can return a pointer to a function of the same type

别来无恙 提交于 2019-12-14 02:16:46
问题 The following is Question 1.22 from the comp.lang.c faq. Why is the definition of ptrfuncptr necessary? typedef int (*funcptr)(); /* generic function pointer */ typedef funcptr (*ptrfuncptr)(); /* ptr to fcn returning g.f.p. */ funcptr start(), stop(); funcptr state1(), state2(), state3(); void statemachine() { ptrfuncptr state = start; while(state != stop) state = (ptrfuncptr)(*state)(); } funcptr start() { return (funcptr)state1; } 回答1: ptrfuncptr is intended to serve as a specific pointer