在函数上使用类型定义
Use of typedef on function
在下面,我如何用typedef语法定义我的函数?
typedef void F();
//declare my function
F f;
//error
F f { }
函数定义将遵循通常的语法:
//declare my function
F f; //it is exactly equivalent to : void f();
//definition
void f() { cout << "hello world"; }
要测试定义确实是先前声明的函数的定义,在声明之后的和定义之前的调用函数f()
(阅读main()
中的注释):
//declaration
F f;
int main()
{
f(); //at compile-time, it compiles because of *declaration*
}
//definition
void f() { std::cout << "hello world" << std::endl; }
Demo: http://ideone.com/B4d95
至于为什么F f{}
不起作用,因为它是语言规范明确禁止的。§8.3.5 (c++ 03)说
函数类型的typedef可用于声明函数,但不能用于定义函数(8.4)。
[Example:
typedef void F();
F fv; // OK: equivalent to void fv();
F fv { } // ill-formed
void fv() { } // OK: definition of fv
—end example]
重要的几点:
- 函数的类型定义可以用来声明一个函数
- 函数的类型定义不能用于定义函数
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 列表参数的类型定义
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 关于 C++ 中的函数类型定义
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 将使用/类型定义限制为类范围
- 模板类型定义?
- C++:模板类的类型定义
- 如何对命名空间限定类型进行类型定义?
- 此递归模板类型定义是否有效C++?
- 具有调整对齐方式的类型定义
- C++从抽象类型定义类成员
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何将result_of与函数类型定义一起使用
- 在C++的适当类型定义位置
- 如何根据模板类型定义浮点常量?
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 参数化类的别名(或类型定义)内部类
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?