What is a function type used for?

北城以北 提交于 2019-12-04 23:42:15

As well as the use you point out (the underlying type of a pointer or reference to a function), the most common uses for function types are in function declarations:

void f(); // declares a function f, of type void()

for which one might want to use a typedef:

typedef void ft(some, complicated, signature);
ft f;
ft g;

// Although the typedef can't be used for definitions:
void f(some, complicated, signature) {...}

and as template parameters:

std::function<void()> fn = f;  // uses a function type to specify the signature

Also consider this

template<typename T>
void f(T*);

Since we want it to accept function pointers, by pattern matching the T becomes a function type.

I just want to point out that I think function_t *fptr ... is clearer than funcptr_t fptr.... The first form stated that it must be a pointer type, which is clearer than anything, including the name funcptr_t.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!