function-pointers

Casting function pointers with different pointer types as an argument

冷暖自知 提交于 2019-12-23 09:31:50
问题 The following code, I think, describes what I am trying to do. Specifically, I wish to cast a function pointer to a generic function type, with the only difference in signature being different pointer types. Now, I'm aware that there is a requirement for function pointers to be compatible as discussed in this question, but I'm not sure whether having an argument of different pointer type satisfies that compatibility requirement. The code compiles and runs, but, as expected, gives warnings

C++ class member pointer to global function

不问归期 提交于 2019-12-23 09:30:03
问题 I want to have a class which has as a member a pointer to a function here is the function pointer: typedef double (*Function)(double); here is a function that fits the function pointer definition: double f1(double x) { return 0; } here is the class definion: class IntegrFunction { public: Function* function; }; and somewhere in the main function i want to do something like this: IntegrFunction func1; func1.function = f1; But, this code does not work. Is it possible to assign to a class member

Is cutting if statements by using function pointers going to be more efficient?

时间秒杀一切 提交于 2019-12-23 09:13:01
问题 So, there's this rule to try to pull if statements out of high repetition loops: for( int i = 0 ; i < 10000 ; i++ ) { if( someModeSettingOn ) doThis( data[i] ) ; else doThat( data[i] ) ; } They say, it's better to break it up, to put the if statement outside: if( someModeSettingOn ) for( int i = 0 ; i < 10000 ; i++ ) doThis( data[i] ) ; else for( int i = 0 ; i < 10000 ; i++ ) doThat( data[i] ) ; (In case you're saying "Ho! Don't optimize that yourself! The compiler will do it!") Sure the

pre-typedef'ing a variadic-function-pointer argument

六眼飞鱼酱① 提交于 2019-12-23 06:57:04
问题 I have a function foo that takes a variadic function pointer as its argument. I would like to use "using" to define the argument's type prior to the function declaration. template <typename ... vARGS> using TFuncType = void(*)(vARGS ... V_args); template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {} void bar(int i) {} int main() { foo(&bar); // This line fails to compile. } This doesn't compile. The error (via clang using c++1z) is: /make/proj/test/variadic-funcparam-deduce2

How to assign functor to function pointer?

心不动则不痛 提交于 2019-12-23 05:41:58
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

How to assign functor to function pointer?

微笑、不失礼 提交于 2019-12-23 05:41:06
问题 Generally, can I assign a function object to a function pointer? I want to do something like this: #include <iostream> class Foo { int num; public: Foo(int num_par) : num(num_par) {} void operator()(int multiplier) { std::cout << multiplier * num << std::endl; } }; int main() { typedef void(*bar)(int); Foo f(42); bar b = f; // MSVC error // ^ C2440: 'initializing' : cannot convert from 'Foo' to 'bar' b(2); // wanted to print 84 } If that's impossible, I’d like an alternative specifically for

How to get the function pointer of a class member in a function of the same class?

北城余情 提交于 2019-12-23 04:58:28
问题 I'm stumped on this. I have a class Foo with a function DoTheThing1 that takes a pointer to a void function with 0 parameters and calls the function. class Foo { public: Foo () {} void DoTheThing1 (void (*theThing)()) { theThing(); } }; I have another class Bar that has an instance of Foo . Class Bar also has its own function DoTheThing2 where it tries in it's construct to pass a pointer of DoTheThing2 to Foo's DoTheThing1 . class Bar { public: Foo* foo = new Foo(); Bar () { foo->DoTheThing1(

How does wrapping a function pointer and function object work in generic code?

╄→гoц情女王★ 提交于 2019-12-23 04:34:17
问题 The following template definition template <typename Func, typename ReturnType, typename... Arguments> class Command { public: Command(Func f) : m_func(f) { } ReturnType operator()(Arguments... funcArgs) { return m_func(funcArgs...); } private: Func m_func; }; gives an error message with gcc 4.7.3 (error: field 'Command::m_func' invalidly declared function type) when instantiated with the following test code: void testFunction(int i, double d) { std::cout << "TestFunctor::operator()(" << i <<

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

谁说胖子不能爱 提交于 2019-12-23 03:06: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

C++ function pointers inside templates

ⅰ亾dé卋堺 提交于 2019-12-23 01:52:49
问题 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"