function-pointers

Should lambda decay to function pointer in templated code?

痴心易碎 提交于 2019-12-05 03:56:51
I read somewhere that a lambda function should decay to function pointer if the capture list is empty. The only reference I can find now is n3052 . With g++ (4.5 & 4.6) it works as expected, unless the lambda is declared within template code. For example the following code compiles: void foo() { void (*f)(void) = []{}; } But it doesn't compile anymore when templated (if foo is actually called elsewhere): template<class T> void foo() { void (*f)(void) = []{}; } In the reference above, I don't see an explanation of this behaviour. Is this a temporary limitation of g++, and if not, is there a

Detecting function object (functor) and lambda traits

北慕城南 提交于 2019-12-05 03:38:06
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 ReturnType; typedef R Signature(); }; template<class R, class P> struct FnTraits<R(*)(P)> { typedef P

Templates, Function Pointers and C++0x

一笑奈何 提交于 2019-12-05 02:25:32
One of my personal experiments to understand some of the C++0x features: I'm trying to pass a function pointer to a template function to execute. Eventually the execution is supposed to happen in a different thread. But with all the different types of functions, I can't get the templates to work. #include <functional> int foo(void) {return 2;} class bar { public: int operator() (void) {return 4;}; int something(int a) {return a;}; }; template <class C> int func(C&& c) { //typedef typename std::result_of< C() >::type result_type; typedef typename std::conditional< std::is_pointer< C >::value,

Function pointer to __attribute__((const)) function?

让人想犯罪 __ 提交于 2019-12-05 01:22:43
How (in GCC/"GNU C") do you declare a function pointer which points to an __attribute__((const)) function? The idea being that I want the compiler to avoid generating multiple calls to the function called through the function pointer when it can cache the return value from a previous call. typedef void (*t_const_function)(void) __attribute__((const)); static __attribute__((const)) void A(void) { } static void B(void) { } int main(int argc, const char* argv[]) { t_const_function a = A; // warning: initialization makes qualified // function pointer from unqualified: t_const_function b = B;

What is a function type used for?

北城以北 提交于 2019-12-04 23:42:15
Given the following two typedef s: typedef void (*pftype)(int); typedef void ftype(int); I understand that the first defines pftype as a pointer to a function that takes one int parameter and returns nothing, and the second defines ftype as a function type that takes one int parameter and returns nothing. I do not, however, understand what the second might be used for. I can create a function that matches these types: void thefunc(int arg) { cout << "called with " << arg << endl; } and then I can create pointers to this function using each: int main(int argc, char* argv[]) { pftype pointer_one

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

女生的网名这么多〃 提交于 2019-12-04 23:26:36
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); } . haccks 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 type argument and return an int . To be more clear: foo -- foo foo( ) -- is a function foo(int) --

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

岁酱吖の 提交于 2019-12-04 22:30:43
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? 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 to decay into a pointer. The pointer should then be dereferenced to obtain the functions address for the call

C++ Syntax/Semantics Question: Reference to Function and typedef keyword

六眼飞鱼酱① 提交于 2019-12-04 22:22:16
问题 What would typedef int (&rifii) (int, int) be used for? What is the typedef before this "statement" do? I want to think of this as typedef (int (&rifii) (int, int)) [new name] but the [new name] is not there like if you do typedef int INTEGER; Similar question for the following syntax: typedef void (*PF) (); PF edit_ops[ ] = { &cut, &paste, &copy, &search }; PF file_ops[ ] = { &open, &append, & close, &write }; PF* button2 = edit_ops; PF* button3 = file_ops; button2[2]( ); What is the typedef

Messy function pointer interpretation

℡╲_俬逩灬. 提交于 2019-12-04 21:29:09
问题 I happen to come across the following function pointer. char (*(*x())[])(); It looks like an array of function pointer in the following format, but I can't see what f -> (*x()) means. How to interpret this messy function pointer? char (*f[])(); ADDED With John Bode's help, I make an example as follows. #include <stdio.h> char foo() { return 'a'; } char bar() { return 'b'; } char blurga() { return 'c'; } char bletch() { return 'd'; } char (*gfunclist[])() = {foo, bar, blurga, bletch}; char (*(

How do you pass a function of a class as a parameter to another function of the same class

ε祈祈猫儿з 提交于 2019-12-04 18:19:57
i basically want to use a dif function to extract a different element of a class (ac). the code is similar to this: .h: class MyClass { public: double f1(AnotherClass &); void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)); }; .cc: double MyClass::f1(AnotherClass & ac) { return ac.value; } void MyClass::f0(AnotherClass & ac, double(MyClass::*f1)(AnotherClass &)) { std::cout << f1(ac); } didn't work, it gives error#547 "nonstandard form for taking the address of a member function" EDIT: I call it from: void MyClass(AnotherClass & ac) { return f0(ac,&f1); // original and