In C++, what’s the difference between an inline function and a macro?
来源:互联网 发布:台湾导弹事件知乎 编辑:程序博客网 时间:2024/05/22 10:53
This question was asked in interviews with both Apple and Intuit.
The major difference between inline functions and macros is the way they are handled. Inline functions are parsed by the compiler, whereas macros are expanded by the C++ preprocessor. This difference creates other differences, as best illustrated by examples.
The C++ preprocessor implements macros by using simple text replacement. Suppose we have the following macro:
#define SUM(a,b) (a+b)
When the preprocessor comes across any occurrences of SUM(first, last) in the code, then that text will be replaced by (first + last). When would one want to use a macro? Usually when what you’re substituting for is very simple, and does not justify the overhead of a function call. Remember that function calls do incur overhead.
Inline functions, as mentioned earlier, are parsed by the compiler directly instead of the preprocessor. Inline functions look very similar to regular functions. Here is what an inline function implementation of the SUM macro would look like:
// note the use of the 'inline' keywordinline int sum(int a, int b){return (a+b);}
The difference between an inline function and a regular function is that wherever the compiler finds a call to an inline function, it writes a copy of the compiled function definition. However, with a regular function, a normal function call is generated.
The reason C++ has inline functions and macros is to eliminate the overhead incurred by function calls. However, the tradeoff of this is the fact that the program size increases with both macros and inline functions. Remember that inline functions look like regular functions, but macros are implemented with text replacement.
The fact that macros use text replacement creates the potential for bugs. Suppose we have the following code:
#define DOUBLE(X) X*X int y = 3; int j = DOUBLE(++y);
If you’re expecting that j will be assigned a value of 4 squared (16), then you would be wrong. Because of the text replacement, what actually happens is that the DOUBLE(++y) expands to ++y * ++y, which equals 4*5, giving us 20. This problem would not occur if DOUBLE were implemented as an inline function. Inline functions only evaluate their arguments once, so any side effects of evaluation happen only once.
Another problem with macros occurs with binding. Suppose we have a macro with two statements, and then we try to use that macro with an if statement. If we decide not to use the curly brackets with our if statement, then we will have something that looks like this:
#define ADD_TWO(x,y) x += 2; y +=2bool flag = true;int j = 5, k = 7;if(flag)ADD_TWO(j,k);Then you’re probably thinking that the macro will expand to this:
if(flag){j +=2;k +=2;}But what actually happens is that the if statement binds to the first expression in the macro. So this is what it really expands to:
if(flag){j +=2;} k +=2;
If we had used an inline function instead of a macro, the problem shown above would not have occurred. This is because an inline function is treated as a single statement, so the entire function would be bound to the if statement.
Debugging macros is also difficult. This is because the preprocessor does the textual replacement for macros, but that textual replacement is not visible in the source code itself. Because of all this, it’s generally considered a good idea to use inline functions over macros.
- In C++, what’s the difference between an inline function and a macro?
- What's the difference between an event and a delegate?
- In SQL, what’s the difference between a full join and an inner join?
- What's the difference between Interface and an abstract class in visual c#?
- macro与inline的区别 What is the difference between macro and inline?
- WHAT'S THE DIFFERENCE BETWEEN AN OEM AND AN ODM?
- What's the difference between an Application, Process, and Services?
- What’s the difference between a class variable and an instance variable?
- What is the difference between an EXE and a DLL?
- What's the difference between a thread and a process?
- What’s the difference between a stack and a heap?
- What's the difference between a URI and a URL?
- What's the difference between LastWriteTime and ChangeTime in FILE_BASIC_INFO?
- What's the difference between Template and Table in Smartforms
- What's the difference between “local” and “my” in Perl
- What's the difference between Future and FutureTask in Java?
- What's the difference between gcc and g++/gcc-c++?
- What is the difference between fprintf and vfprintf in C++?
- xjc-beyond sliding windows-Object Localization by Efficient Subwindow Search
- xcode6中添加pch文件
- asp.net与Matlab类型转换(待补全)
- Vs工程高版本向低版本迁移
- Multithreaded simple data type access and atomic variables
- In C++, what’s the difference between an inline function and a macro?
- 字符串数组转化成十六进制字符串
- eclipse 使用Maven deploy命令打包源码到私服nexus
- 先贴上代码:Random快排,快排的非递归实现
- JDBC对象
- JS 判断上传 文件 大小
- 机器学习xgboost实战—手写数字识别
- 如何确保项目开发敏捷和安全两者兼得呢?
- LeetCode Peeking Iterator