Inline functions
Problems: (Page 372)
There are two problems with the use of proprocessor macros in C++. The first is also with C: a macro looks like a function call, but does not act like one. This can bury difficult-to-find bugs. The second problem is specific to C++: the preprocessor has no permission to access class member data. This means preprocessor macros cannot be used as class member functions.
There are many subtle bugs examples with MACRO in Page 372.
Inline functions:
Any function defined within a class body is automatically inline, but you can also make a non-class function inline by preceding it with the inline keyword. You must include the function body with the declaration, otherwise the compiler will treat it as an ordinar function declaration.
Inline function implementation mechanism: (Page 377)
You will almost want to put inline definitions in a header file. When the compiler sees such a definition, it puts the function type and the function body in its symbol table. When you use the function, the compiler checks to ensure the call is correct and the return value is being used correctly, and then substitutes the function body for the function call, thus eliminating the overhead. The inline code does occupy space, but if the function is small, this can actually take less space than the code generated to do an ordinary function call.
The following rules of thumb might help:
inline function must be small;
access function can be inline function.
Limitations: (Page 390)
There are two situations in which the compiler cannot perform inlining.
Firstyly, the compiler cannot perform inlining if the function is too complicated.
Secondly, the complier also cannot perform inlining if the address of the function is taken implicitly or explicitly.
Three special features in the C preprocessor:
stringizing #define DEBUG(x) cout << #x " = " << x << endl
string concatenation
token pasting
#define FIELD(a) char* a##_string; in a##_size
class Record{
FIELD(one);
FIELD(two);
FIELD(three);
};
Inline functions的更多相关文章
- why inline functions must be put in header files?
[why inline functions must be put in header files?] 编译中有2个过程:compile.link.先进行compile,compile中把源代码编译成 ...
- C++对象模型——Inline Functions(第四章)
4.5 Inline Functions 以下是Point class 的一个加法运算符的可能实现内容: class Point { friend Point operator+(const Poin ...
- 内联函数(Inline Functions)
影响性能的一个重要因素是内联技巧.内联函数也可称为内嵌函数. 在C++中,函数调用需要建立栈环境,进行参数复制,保护调用现场,返回时,还要进行返回值复制,恢复调用现场.这些工作都是与完成特定任务的操作 ...
- The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
https://stackoverflow.com/questions/30045871/sorting-the-view-based-on-frequency-in-sql-server Just ...
- C++ inline weak symbol and so on
关于inline这个关键字,听到强调得最多的是,它只是一种对于编译器的建议,而非强制执行的限定. 但事实上,即使这个优化最终由于函数太过复杂的原因没有达成,加上inline关键字(还有在类定义中直接定 ...
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
- (转) Functions
Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...
- About Why Inline Member Function Should Defined in The Header File
About why inline member function should defined in the header file. It is legal to specify inline on ...
- 什么是内联函数(inline function)
In C, we have used Macro function an optimized technique used by compiler to reduce the execution ti ...
随机推荐
- 10个强大的Apache开源模块
1.单点登录模块 LemonLDAP LemonLdap可以很棒地实现Apache的SSO功能,并且可以处理超过 20 万的用户请求.LemonLdap支持Java, PHP, .Net, Perl, ...
- IE-首页跳转到 q160的问题解决
IE首页跳转到 q160的问题解决 服了又中找了,IE快捷方式被 www.q160.com劫持 该死的这个网站什么也没有做,就是做了一个google搜索的连接. 进行了一次搜索 http: ...
- DataTable 导出到 Excel 类
底层类: #region DataTable 导出到 Excel /// <summary> /// DataTable 导出到 Excel /// </summary> // ...
- 用HiveDB横向切分MySQL数据库
HiveDB是一个用来横向切分mysql数据库的开源框架,构建一个高性能和可扩展的基于mysql的系统需要大量的系统设计经验和良好的代码的实现,一个比较好的策略是将你的数据横向切分在多个server上 ...
- 树莓派(Rospberry Pi B+)到货亲測
1 图鉴 Rospberry Pi B+最终在今天下午有蜗牛快递公司圆*送到了.B+主要是添加了2个USB,添加了GPIO,sd卡换成了micro sd ...先不说直接上图再说,期待了好久好久 w ...
- JSF学习四 标签
commandButton:提交.重置或下压button button:用于公布GET请求的按钮
- NSUInteger和NSInteger和int
int和NSInteger其实是差不多的,但更推荐使用NSInteger,因为使用NSInteger,这样就不用考虑设备是32位还是64位了. NSUInteger是无符号的,即没有负数,NSInte ...
- linux find命令详解--转
转自:http://blog.csdn.net/jakee304/article/details/1792830 (一)Get Start 最简单的find用法莫过于如此: $ find . 查找当前 ...
- GitHub干货分享(APP引导页的高度集成 - DHGuidePageHUD)
每一个APP都会用到APP引导页,分量不重但是不可缺少,不论是APP的首次安装还是版本的更新,首先展现给用户眼前的也就只有它了,当然这里讲的不是APP引导页的美化而是APP引导页的高度集成,一行代码搞 ...
- MyEclipse的快捷键的使用
MyEclipse的10个快捷键:Ctrl + Shift + T: 打开类型:显示"打开类型"对话框来在编辑器中打开类型."打开类型"选择对话框显示工作空间中 ...