-
Notifications
You must be signed in to change notification settings - Fork 3k
header files should not have using
statements (namespaces)
#5679
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
Comments
using
statements (namespaces)
This has been discussed few times, you can find an answer here: https://os.mbed.com/questions/5492/c-design-pattern-or-problem/
Only cc @sg- |
Do you mean that you agree with me that the
It should probably also be added to the contributor guidelines not to put |
@0xc0170, what is the meaning of the "type: question" label? |
I labelled it as question initially to answer why there is using statement in the main header file. Once you shared there are few more, this can be considered as a bug, and we will need to clean those. |
@bmcdonnell-ionx
I believe However we may question
Similarly we may question inclusion of modules headers (mbed.h, mbed_events.h and rtos.h) in individual headers files. Professional users should be able to cherry pick the definitions they need without polluting the global namespace. @0xc0170 Is there a clear reason to include the std namespace in |
On closer inspection, it looks like these
(Emphasis mine.) I don't think the modest gain for beginners here is worth the cost to others. Besides, won't modifying the examples to have the using directives (or to fully specify identifiers) be sufficient guidance?
Obviously I agree that these should go away. (Included for completeness.)
What do you mean by "inclusion of modules headers...in individual header files"?
|
Here is an example with the ATParser header. It is not possible to import definitions present in that header without polluting the global namespace because it includes |
I'm still not clear on what you're saying.
What would be an example of "importing a definition"? ...I'm not a fan of the single monolithic header file ( |
What does this mean? Also, what do the |
That this has been added to our internal system. Nothing to be worry about |
The hard answer: Backwards compatibility. There's no path for removing the using statements from
This was the original idea I believe, and I think it was founded on a good idea. But it does cause problems. What can we do? I've been mulling over this thought for a while, and I think there is a path to fix the namespace. It may take a bit of work to implement, but I think the improved developer experience would be worth it:
Afterwards, this: #include "mbed.h"
blahblahblah Turns into this: #include "mbed-os.h"
using namespace mbed;
blahblahblah Or something like this: #include "mbed-os.h"
using mbed::NetworkInterface;
using mbed::TCPSocket;
blahblahblah This would be a big change and would probably require someone dedicated to the task for a stretch of time, but it would remove concerns with name collisions. It would also present a more standard C++ API, and help establish the boundary between what is an mbed API and what is a target or user provided component. This would be a big enough change that it may be worth considering for an mbed OS 6.0.0 release. |
Example further motivation: I was just stumped for several minutes trying to forward declare |
@geky I'm not sure the discussion will lead somewhere
From my perspective, the issue in that case is the use of Unfortunately uses of About the changes proposed, I personally believe that even if
I would argue the contrary, nested namespace offers a clear separation between modules of the OS and prevent name clashes between them. It is easier to find an information if it's organized in cohesive collections of elements. |
What do you think is the purpose of |
Such facility increases builds time for very little benefits. It may be OK for some projects but not all of them. As an example consider the numbers given in the blog post covering the release of mbed os 5.7:
If using More generally, if we have a global headers use cases well as dos and don'ts should be documented. User can make their choices based on these information. |
Oh, good points. I actually alluded to this earlier, when I said,
Since you brought it up again, I'd just as soon see Of course, documentation would need to be updated accordingly. A list of modules and corresponding header names to |
ARM Internal Ref: MBOTRIAGE-791 |
@deepikabhavnani, please re-open. Several
|
@bmcdonnell-ionx - Sorry missed few, but from previous conversation, below will not be fixed till major version release for backward compatibility. ./mbed.h:using namespace mbed; |
@deepikabhavnani, should one of us open a new issue for that after you fix the others? Or should this issue stay open for those? |
Let this issue be open. I don;t think "./features/unsupported/dsp/dsp/dsp.h:using namespace dsp;" this will be fixed as it is unsupported. |
Pending ones listed below:
|
What I mean is, after those 4 are fixed, should this issue still remain open due to the |
ohh.. Yeah it would be good to have new issue (enhancement) for mbed.h change. Thanks :-) ./rtos/rtos.h:using namespace rtos; |
So these two remain on this issue. (1) In an effort to wrap this up... The Re (1), how do I build and run the cfstore tests to test this? I tried
|
Thanks @bmcdonnell-ionx for the summary , we should address both. |
Internal Jira reference: https://jira.arm.com/browse/IOTCORE-525 |
@bmcdonnell-ionx - #7760 should resolve this issue. All using statements cannot be removed from all header files for backward compatibility, but you can add 'MBED_NO_GLOBAL_USING_DIRECTIVE Closing this. Please reopen new issue, if more namespace issues are encountered. Thanks for your patience and support in resolving this. |
Description
Several mbed-os header files have
using
statements in them. Most notably inmbed.h
:Reason to enhance or problem with existing solution
There are plenty of online references explaining why having
using
statements in header files is bad practice. In brief, it pollutes the global namespace for your users.Suggested enhancement
All
using
statements should be removed from all header files.Pros
Don't pollute your users' namespaces, forcing them to change their module names or create namespaces to avoid name collisions with your stuff.
Also, this gives users the option of always having namespace prefixes on things if they want it - and having the compiler enforce that - if they find that useful, in whatever situations.
Cons
Granted, it is a painful change for users who relied on it, so a migration plan is appropriate. e.g. Maybe explain the change in the release notes in mbed-os 5.7 or 5.8 or 6.0 that these
using
statements have been removed, and users can add the statements to their source files if they wish to minimize code changes.The text was updated successfully, but these errors were encountered: