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 ...
随机推荐
- HW4.42
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- (android 源码下开发应用程序) 如何在 Android 各 level ( 包含 user space 與 kernel space ) 使用dump call stack的方法
http://janbarry0914.blogspot.com/2014/07/androiddump-call-stack.html dump call stack [文章重點] 了解 Andro ...
- 四舍六入五成双银行家算法的 PHP和Javascript实现
四舍六入五成双 http://baike.baidu.com/view/1245064.htm?fr=aladdin 四舍六入五成双是一种比较精确比较科学的计数保留法,是一种数字修约规则. 对于位数很 ...
- IE-首页跳转到 q160的问题解决
IE首页跳转到 q160的问题解决 服了又中找了,IE快捷方式被 www.q160.com劫持 该死的这个网站什么也没有做,就是做了一个google搜索的连接. 进行了一次搜索 http: ...
- GCC扩展 __attribute__ ((visibility("hidden")))
试想这样的情景,程序调用某函数A,A函数存在于两个动态链接库liba.so,libb.so中,并且程序执行需要链接这两个库,此时程序调用的A函数到底是来自于a还是b呢? 这取决于链接时的顺序,比如先链 ...
- Ajax核心——XMLHttpRequest基础
XMLHttpRequest对象负责将用户信息以异步方式发送到服务器,并接受服务器返回的相应信息和数据.也就是可以在页面已经加载后从后从服务器请求.接收数据,这样使得用户的体验度更好,而同时提升了客户 ...
- Jquery实现图片轮播源码
<html><head><style type="text/css">#banner {position:relative; width:478 ...
- solr 搜索引擎
http://www.cnblogs.com/wenxinghaha/p/4088790.html
- 鼠标移动事件--JavaScript
鼠标移动事件 <html> <head> <title>鼠标移动事件</title> </head> <style type=&quo ...
- 从 setNeedsLayout 说起
本文从 setNeedsLayout 这个方法说起,分享与其相关的 UIKit 视图交互.使用场景等内容. UIKit 为 UIView 提供了这些方法来进行视图的更新与重绘: public func ...