function-pointers

Lua - Execute a Function Stored in a Table

和自甴很熟 提交于 2019-12-07 03:20:52
问题 I was able to store functions into a table. But now I have no idea of how to invoke them. The final table will have about 100 calls, so if possible, I'd like to invoke them as if in a foreach loop. Thanks! Here is how the table was defined: game_level_hints = game_level_hints or {} game_level_hints.levels = {} game_level_hints.levels["level0"] = function() return { [on_scene("scene0")] = { talk("hint0"), talk("hint1"), talk("hint2") }, [on_scene("scene1")] = { talk("hint0"), talk("hint1"),

C++ function pointers inside templates

霸气de小男生 提交于 2019-12-07 00:54:36
I have this problem: template<typename T> class Bubu { ... int (*comparer)(const T t1, const T t2); ... public: Bubu(int (*_comparer)(const T t1, const T t2)) { comparer = _comparer; } }; And in another file: Bubu<char*> asd(strcmp); Error: error C2664: 'Bubu<T>::Bubu(int (__cdecl *)(const T,const T))' : cannot convert parameter 1 from 'int (__cdecl *)(const char *, const char *)' to 'int (__cdecl *)(const T,const T)' I don't understand why. Shouldn't the compiler see a "char*" instead of "T" there? EDIT: the Ideone.com-ready code: int asdf(const char* a, const char* b) { return 0; } template

Detecting function object (functor) and lambda traits

拜拜、爱过 提交于 2019-12-06 22:15:49
问题 How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)? Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open to supplementing or replacing them. I could do something like this: namespace nsDetail { class Dummy { Dummy(); }; } template<class Fn> struct FnTraits; template<class R> struct FnTraits<R(*)()> { typedef nsDetail::Dummy ParamType; typedef R

C: What even is function pointer conversion? [closed]

自古美人都是妖i 提交于 2019-12-06 20:29:30
Closed . This question needs details or clarity . It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post . Closed last year . Let's say one wanted to create an array that could hold multiple function pointers & of different types .. how would he go about doing so ? Perhaps an array of void pointers could work ?... Well as it turns out, no since in order to use the functions stored in the void pointers you'd have to convert/cast them (the void pointers) back to function pointers and... "A conversion between a function

Pointers to different instances of one templated function guaranteed to compare unequal?

孤街浪徒 提交于 2019-12-06 20:14:50
问题 Is it safe to assume that two function pointers, that point to different instances of one templated function, will compare unequal? Even if the templated function does not use the template parameter(s) at all and thus does the exact same thing in every case? For example the following works fine on my compiler but I'm not sure if it does on any other: class TypeChecker { public: template< typename T > static void foo( void ) {} template< typename T > static void setType( void ) { s_fooPtr =

Dereferencing a function with default arguments - C++14 vs C++11

大城市里の小女人 提交于 2019-12-06 18:33:24
问题 Following code can't be compiled with g++ version 5.4.0 with option -std=c++1y : void f(int=0) ; int main() { f(); // ok (*f)(2);// ok (*f)();// ok c++11; error with c++14: too few arguments to function return 0; } The function declared to have default argument, so what is wrong here? thanks for help. And why does g++ -c -std=c++11 compile? 回答1: Accepting (*f)() as valid is a GCC bug. The letter of the standard indicates that using a function name with unary * should cause the function name

What does this weird function pointer declaration in C mean? [duplicate]

元气小坏坏 提交于 2019-12-06 18:03:15
问题 This question already has answers here : Writing a function pointer in c (3 answers) How do you read C declarations? (10 answers) Closed 4 years ago . Can anyone please explain what int ((*foo(int)))(int) in this does? int (*fooptr)(int); int ((*foo(int)))(int); // Can't understand what this does. int main() { fooptr = foo(0); fooptr(10); } . 回答1: int ((*foo(int)))(int); This declares foo as a function that expects an int type argument and returns a pointer to a function that expects an int

p is a pointer to a structure, what do all these code snippets do?

余生长醉 提交于 2019-12-06 16:14:17
++p->i p++->i *p->i *p->i++ (*p->i)++ *p++->i I don't understand these statements above, I wrote a small test program to understand them. #include <stdio.h> struct my_structure { int i; }; void main() { struct my_structure variable = {20}; struct my_structure *p = &variable; printf("NAME: %d\n", ++p->i); printf("NUMBER: %d\n", p++->i); printf("RANK: %d", *p->i++); printf("name: %d\n", *p->i++); printf("number: %d\n", (*p->i)++); printf("rank: %d", *p++->i); } Here's what the output I got after I commented the last four print statements: NAME: 21 NUMBER: 21 And after uncommenting the code and

How to call pointer to function defined in typedef struct

不打扰是莪最后的温柔 提交于 2019-12-06 15:47:19
what is wrong with the following code? parseCounter1() and parseCounter1() below are two functions. I put their pointers in const OptionValueStruct so that they can be called accordingly when each element of option_values[] are gone through: typedef struct OptionValueStruct{ char counter_name[OPTION_LINE_SIZE]; int* counter_func; } OptionValueStruct_t; const OptionValueStruct option_values[] = { {"Counter1", (*parseCounter1)(char*, char**)}, {"Counter2", (*parseCounter2)(char*, char**)}, }; const OptionValueStruct *option = NULL; for(int i = 0; i< sizeof(option_values)/sizeof(OptionValueStruct

C++ code generation: create a factory for function pointers

不羁的心 提交于 2019-12-06 13:58:55
问题 I have a long and steadily growing list of (non-member) functions and I need to select one of the functions from this list at runtime (based on a command line argument). At the moment I do this using a factory function which takes a string (the name of the function) and returns a pointer to the function. However this means I have to edit the factory function every time I add a new function (which is both annoying and a violation of the DRY principle). I would like to somehow generate the