Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
CDT/Obsolete/C editor enhancements/Namespaces and 'using' management
This is a problem page. Please treat it as a discussion page and feel free to insert your comments anywhere. My open questions are in bold--Tomasz Wesołowski 16:21, 30 April 2010 (UTC)
Description
Manual management of namespaces in C++ is something which can be handled easily, but still requires substantial amount of unnecessary typing. Some enhancements should be possible to be introduced here with little work.
Ranking
5/5 Sergey Prigogin 07:17, 18 May 2010 (UTC)
1/5 Tobias Hahn 09:33, 18 May 2010 (UTC)
4/5 Jens Elmenthaler
4/5 Kirstin Weber 08:37, 10 June 2010 (UTC)
Solutions
Symbols in namespaces can be referenced in the following ways:
-
using namespace X
introduces all symbols from the namespace into a given scope -
using X::symbol
introduces only one symbol into a given scope - literally typing
X::symbol
in code
Which do you use by coding standard in your production code?
I believe it's clear that The IDE should not enforce any specific solution.
In our code base using namespace X
is prohibited, using X::symbol
should be used in source files, X::symbol
should be used in header files (Sergey Prigogin).
Jens Elmenthaler We use using namespace ::X
in source files, using ::X::symbol
in header files if contained in another namespace, and ::X::symbol
in header files on global scope. Note the leading ::
. This is needed if you use nested namespaces.
Context assist
An idea is to let Context Assist display all symbols regardless of their namespace, with the namespace itself written next to the suggestion. (Note that it's similar to the current behaviour of JDT and packages.)
When the user selects a suggestion from Context Assist, the IDE should ensure that the namespace of this symbol will be included. One possibility is to create a Preference for selection of the expected behaviour:
- insert the symbol and add an 'using' directive in this module
- insert the symbol preceeded by explicit namespace reference - i.e.
SomeC[ctrl+space]
could be expanded tofoo::bar::SomeClass
. - require an explicit reference - i.e.
SomeC[ctrl+space]
would not yield results and the programmer would be required to explicitly ask forfoo::bar::SomeC[ctrl+space]
. This option would be a possibility to revert to the current solution.
The using namespace X
variant probably doesn't need to be automated, as the programmer would have to specify such decision explicitly anyway. It should be noted that the completions mentioned above should only be performed if there's the need to do so in current context - i.e. typing the 'using' clause would cause that the IDE wouldn't prefix a symbol name with its namespace even if the second mentioned variant would be set in preferences.
References
Bug 182897 critiques the method of resolving namespaces by adding "using namespace X" along with the include.