Skip to content

Commit 91298f2

Browse files
committed
Remove the content of the contrast between inline functions and macros
1 parent ae7e3ca commit 91298f2

File tree

2 files changed

+1
-56
lines changed

2 files changed

+1
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ _themes.VS.Modern/
1414
# Documentation build
1515
/docs/vcppdocs
1616
debug.log
17+
/.vs

docs/cpp/inline-functions-cpp.md

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -202,62 +202,6 @@ Assuming coordinate manipulation is a relatively common operation in a client of
202202

203203
- Return
204204

205-
## Inline functions vs. macros
206-
207-
Inline functions are similar to macros, because the function code is expanded at the point of the call at compile time. However, inline functions are parsed by the compiler, and macros are expanded by the preprocessor. As a result, there are several important differences:
208-
209-
- Inline functions follow all the protocols of type safety enforced on normal functions.
210-
211-
- Inline functions are specified using the same syntax as any other function except that they include the **`inline`** keyword in the function declaration.
212-
213-
- Expressions passed as arguments to inline functions are evaluated once. In some cases, expressions passed as arguments to macros can be evaluated more than once.
214-
215-
The following example shows a macro that converts lowercase letters to uppercase:
216-
217-
```cpp
218-
// inline_functions_macro.c
219-
#include <stdio.h>
220-
#include <conio.h>
221-
222-
#define toupper(a) ((a) >= 'a' && ((a) <= 'z') ? ((a)-('a'-'A')):(a))
223-
224-
int main() {
225-
char ch;
226-
printf_s("Enter a character: ");
227-
ch = toupper( getc(stdin) );
228-
printf_s( "%c", ch );
229-
}
230-
// Sample Input: xyz
231-
// Sample Output: Z
232-
```
233-
234-
The intent of the expression `toupper(getc(stdin))` is that a character should be read from the console device (`stdin`) and, if necessary, converted to uppercase.
235-
236-
Because of the implementation of the macro, `getc` is executed once to determine whether the character is greater than or equal to "a," and once to determine whether it's less than or equal to "z." If it is in that range, `getc` is executed again to convert the character to uppercase. It means the program waits for two or three characters when, ideally, it should wait for only one.
237-
238-
Inline functions remedy the problem previously described:
239-
240-
```cpp
241-
// inline_functions_inline.cpp
242-
#include <stdio.h>
243-
#include <conio.h>
244-
245-
inline char toupper( char a ) {
246-
return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a );
247-
}
248-
249-
int main() {
250-
printf_s("Enter a character: ");
251-
char ch = toupper( getc(stdin) );
252-
printf_s( "%c", ch );
253-
}
254-
```
255-
256-
```Output
257-
Sample Input: a
258-
Sample Output: A
259-
```
260-
261205
## See also
262206

263207
[`noinline`](../cpp/noinline.md)\

0 commit comments

Comments
 (0)