-
Notifications
You must be signed in to change notification settings - Fork 963
Remove the content of the contrast between inline functions and macros #4823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Learn Build status updates of commit 91298f2: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
@microsoft-github-policy-service agree |
@Mq-b : Thanks for your contribution! The author(s) have been notified to review your proposed change. |
Can you review the proposed changes? When the changes are ready for publication, add a #label:"aq-pr-triaged" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree with this change. The current text is intended to make a contrast that it does very well. There are some additional contrasts that could be made, in particular,
- The
inline
modifier, not unlike theregister
modifier, is a suggestion and need not be honored by the compiler depending on technical determinations. There are also technical limitations. - An
inline
procedure may remain available for reference by a pointer and that can be passed as a parameter. (This might cause an out-of-line version along withinline
instances.) - (perhaps) there can be some involvement of link-time code generation also.
But overall, I would leave the current text alone.
PS: In some sense, a pre-processor function-form definition has its usages mostly in-line, although there are other quirks (including how references to variables are resolved). And they are never external although shareable in headers. Appearing to point to/through (the expansion of) one is very quirky.
Seems plausible, but the points are rather weak.
However, the text should be preserved for following reasons:
The current text should be improved to address the points above explicitly in addition to the code example. Besides, the necessity of NOTE: There are some substantially more serious PL-theoretical topics beyond the languages discussed here. This makes a closer of applicative functions to operative ones (generalized macros). In paticular, consider purely functional languages like pure lambda calculi, whose functions (the semantic objects introduced by syntactic forms named lambda abstrctions) can cover both kinds. In such cases, preprocessors, or even the function-like macros themselves, are implementation details which only show effect in some static phases (of limitations such as a macro must be expanded before some other evaluations of ordinary expressions including function calls). I'm not that interested to expand the topic more here; anyway, it should be a practice left to designers, not ordinary users, of languages. |
That first line of the mentioned page is more objectionable. I would say the inline modifier is a suggestion that usage of a function be made as fast as possible, often leading to usages being expanded in place, avoiding the passing of parameters and return values when employing a single implementation. With regard to contrasting with use of macros, the term @FrankHB These pages are devoted to the use of VC/C++ on the Windows platform and are related to the Microsoft compiler support of C/C++. As an early adherent of functional programming, I am not confused on the matter and any PL discussion seems very off-topic. |
I think we should pay more attention to the meaning of inline keyword in C++ language level itself, the document used to inline modified functions and macros compared, itself is a little strange, because first of all, the language level does not have this meaning, and secondly whether it is completely inline is not necessarily, the document to express unless the function must be inline and macro comparison. Then the example is not good enough.
This is probably more of a historical issue, but this page is in the C++ section, so I think we can simply delete this description. We should even add more description for inline functions, like cppreference |
I did check that at your suggestion. Thanks for that. It is a little odd because (since C++17 evidently) any function defined in a translation unit can be in-lined although giving permission (per cppreference) is a bit strange. Of course there are various constraints and I suppose diagnostics when a translation unit violates a constraint. The conditions on multiple translation units must be interesting to check (or not). I'm not certain that, at a general level, there is much difference between "suggesting" and "permitting." It would be interesting to know what the impact was and are for Microsoft implementations, although I tend to be more keen on portable interpretations. |
I think we should go back to the original question. I think the description of this paragraph is not good and appropriate, and it can be deleted directly. The discussion should be whether to remove, or modify, or add certain descriptions. |
I thought that is what we have been doing. I have a follow-up observation about the cppreference explanation though. It strikes me that what it says about method implementations there is not always true, thinking of references to instances of virtual classes and, of course, interfaces. I suppose it matters when methods are used internally to the instance. In any case, the use of "macro" with regard to the preprocessor-defined function forms is not objectionable. Is the objection to the examples and could they be done better? Is the objection to the statement that inline always causes in-lining? |
There is nothing wrong with this example.
Yes.Neither the C++ standard, the C standard, gcc, clang, msvc, has ever said that its various inline methods, compiler extensions, are mandatory. Going back to the original question, I admit that there is some truth to the msvc documentation and the contrast between macros and inline functions, but in general, I still think it's all historical. And in the current example: inline char toupper( char a );
The toupper function, I think more attention should also be paid to what 'inline' means at the language level. And the contrast in the document is the default: 'inline' keyword modified functions must be inline. However, I would like to delete the section 'Inline functions vs. macros'. Although there is a certain degree of rationality, the two are still completely different things, and the similarities mainly lie in the non-linguistic level. |
I hate to disappoint you. cppreference is not authoritative. The use of the term "macro" for preprocessor-defined symbols and function forms has been standard language since Kernighan & Ritchie. I don't have my hands on Stroustrup but every ISO standard I have seen uses the term and all of the C++ books I just grabbed off my shelves use the term "macro" very consistently. Also, there is use of the term "macro" at cppreference preprocessor. I agree one does not need to know about the preprocessor introduction of in-line code in order to understand the inline modifier. |
So what are you going to do? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should drop a valuable paragraph.
.gitignore
Outdated
@@ -14,3 +14,4 @@ _themes.VS.Modern/ | |||
# Documentation build | |||
/docs/vcppdocs | |||
debug.log | |||
/.vs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be an unrelated change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have deleted
@@ -202,62 +202,6 @@ Assuming coordinate manipulation is a relatively common operation in a client of | |||
|
|||
- Return | |||
|
|||
## Inline functions vs. macros | |||
|
|||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep this paragraph.
It seems to me that only "Inline functions are similar to macros" needs to be improved. They are very different to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also include the fact that inline substitution is not mandatory, it occurs at the compiler's discretion, in the list. Currently the wording is confusing. Inline functions are similar to macros, because the function code is expanded at the point of the call at compile time.
sounds like the substitution always happens.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codeworm96 Your idea is also my idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that only "Inline functions are similar to macros" needs to be improved.
I agree. The statement is not that rigorous, and this causal relationship is also kind of far-fetched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inline functions are similar to macros, because the function code is expanded at the point of the call at compile time.
I think we can just delete this paragraph and keep the last one:
inline functions are parsed by the compiler, and macros are expanded by the preprocessor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this paragraph with:
Inline functions vs. macros
inline functions are parsed by the compiler, and macros are expanded by the preprocessor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original wasn't good, and I'm not sure this is an improvement. 'Parsed by the compiler', although technically true, only indirectly conveys useful information. The crux of the matter that we are trying to get at is that the compiler, at its discretion, actually compiles the function and drops the code of the body into your code without the overhead of a function call, and with the benefit of actually going through the compiler so that language syntax and semantics are properly observed.
I see what the original author was trying to do here, but I think orienting the discussion about what an inline function is by comparing them with macros hurts more than it helps. I'll take a whack at redoing this section and we can see if it's an improvement.
@orcmid I think this paragraph from @FrankHB's comments is related and helpful, which should help to explain why inline functions are considered similar to macros:
|
Learn Build status updates of commit 93025d3: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
PRMerger Results
|
…cpp-docs_inline into Inline-functions-vs-macros
Learn Build status updates of commit 994d366: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
PRMerger Results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your comments and agree. I'll take a shot this week and we'll see if we can get it in a better place. I'll edit in this PR if alright with you.
.gitignore
Outdated
@@ -13,4 +13,4 @@ _themes.VS.Modern/ | |||
|
|||
# Documentation build | |||
/docs/vcppdocs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this change about? Can you revert this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your reminding.
Didn't notice a blank line at the end, deleted.
@@ -202,62 +202,6 @@ Assuming coordinate manipulation is a relatively common operation in a client of | |||
|
|||
- Return | |||
|
|||
## Inline functions vs. macros | |||
|
|||
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original wasn't good, and I'm not sure this is an improvement. 'Parsed by the compiler', although technically true, only indirectly conveys useful information. The crux of the matter that we are trying to get at is that the compiler, at its discretion, actually compiles the function and drops the code of the body into your code without the overhead of a function call, and with the benefit of actually going through the compiler so that language syntax and semantics are properly observed.
I see what the original author was trying to do here, but I think orienting the discussion about what an inline function is by comparing them with macros hurts more than it helps. I'll take a whack at redoing this section and we can see if it's an improvement.
Learn Build status updates of commit 1783fc0: ❌ Validation status: errorsPlease follow instructions here which may help to resolve issue.
For more details, please refer to the build report. Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them. For any questions, please:
|
Take a shot at updating the macro/inline function comparison
Learn Build status updates of commit 4b2340d: ❌ Validation status: errorsPlease follow instructions here which may help to resolve issue.
For more details, please refer to the build report. Note: Your PR may contain errors or warnings or suggestions unrelated to the files you changed. This happens when external dependencies like GitHub alias, Microsoft alias, cross repo links are updated. Please use these instructions to resolve them. For any questions, please:
|
@Mq-b, @orcmid, @frederick-vs-ja, @codeworm96, et. al: I made a quick update for the macro/inline function comparison section tonight, and a couple other places as I read through the article. Let me know what you think. |
Learn Build status updates of commit 9d3f5d3: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
PRMerger Results
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a lot of material. I think there are some confusing wordings. And in the example of
#define mult(a, b) a * b
In the explanation of why this is not the same as multiply(a, b), it is valuable to point out the more-careful
recommended practice,
#define mult(a, b) ((a) * (b))
I will review more closely in my morning afternoon.
Regarding the best practice, my goal isn't to divert into best practices for macros, but show how things can go awry between using a macro and a function. |
@TylerMSFT > Regarding the best practice, my goal isn't to divert into best practices for macros, but show how things can go awry between using a macro and a function. OH. I thought showing that companion example would help folks understand how it is that cases like mult(a, b) get into trouble. Just for clarity of that particular one. |
Some edits. Incorp ocrmid's feedback.
@orcmid, I incorporated a nod to your macro definition best practices suggestion. |
Learn Build status updates of commit 14ca930: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
PRMerger Results
|
small edits
Learn Build status updates of commit f9fee1a: ✅ Validation status: passed
For more details, please refer to the build report. For any questions, please:
|
PRMerger Results
|
#sign-off |
Inline functions and macros have nothing to do with each other, and this description can simply be removed.
cppreference doesn't mention macros.