LPr*_*Prc 6 c typedef function
我有一段代码,我不明白那一个typedef:
typedef void (inst_cb_t) (const char*, size_t);
Run Code Online (Sandbox Code Playgroud)
不,实际上意味着你可以使用inst_cb_t的void呢?但是第二个括号中的内容呢?
hac*_*cks 12
声明
typedef void (inst_cb_t) (const char*, size_t);
Run Code Online (Sandbox Code Playgroud)
定义inst_cb_t为一个带有两个类型const char*和size_t和的参数的函数void.
这个声明的一个有趣的部分是你只能在函数声明和指向函数减速的指针中使用它.
inst_cb_t foo;
Run Code Online (Sandbox Code Playgroud)
你不能在函数定义中使用它
inst_cb_t foo // WRONG
{
// Function body
}
Run Code Online (Sandbox Code Playgroud)
看看C标准:
函数定义中声明的标识符(函数的名称)应具有函数类型,由函数定义的声明符部分指定.162)
和脚注162是
意图是函数定义中的类型类别不能从以下内容继承
typedef:Run Code Online (Sandbox Code Playgroud)typedef int F(void); // type F is ‘‘function with no parameters // returning int’’ F f, g; // f and g both have type compatible with F F f { /* ... */ } // WRONG: syntax/constraint error F g() { /* ... */ } // WRONG: declares that g returns a function int f(void) { /* ... */ } // RIGHT: f has type compatible with F int g() { /* ... */ } // RIGHT: g has type compatible with F F *e(void) { /* ... */ } // e returns a pointer to a function F *((e))(void) { /* ... */ } // same: parentheses irrelevant int (*fp)(void); // fp points to a function that has type F F *Fp; // Fp points to a function that has type F