How to "Unuse" a Namespace

How can I unuse a namespace?

Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...

namespace codegear {
#include "codegear_header.h"
} // namespace codegear

...then the effects of any using directives within that header are neutralized.

That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.

Can one stop using a namespace within one's code?

Take a look at the C++ documentation on Namespaces.

http://www.cplusplus.com/doc/tutorial/namespaces/

You can declare that you are 'using' a namespace for a specific scope (such as a function). But I don't believe you can arbitrarily specify where a using statement starts and ends, it is always for the rest of the scope it is declared in.

I should note (someone please correct me if I'm wrong) that in C++ you can arbitrarily declare a scope almost anywhere you want, simply by using curly braces.

{ 
using mynamespace;
/* rest of your code here */
}

But use it sparingly and carefully, because if used too often or in the wrong context, it can make code harder to read (which defeats the whole point of 'using' in the first place, right?).

Can I undo the effect of using namespace in C++?

No, but you can tell your coworkers that you should never have a using directive or declaration in a header.

Is there a way to not use an item from a namespace?

You cannot do that (include everything and then selectively exclude something).

Your options are:

1) always explicitly qualify names. Like std::vector<int> v;

2) pull in all names with using namespace std;

3) pull in just the names you need with, for example, using std::vector; and then do vector<int> v; - names other than "vector" are not pulled in.

Note: using namespace std; doesn't have to go at global scope and pollute the entire file. You can do it inside a function if you want:

void f() {
using namespace std;
// More code
}

That way, only f() pulls in all names in its local scope. Same goes for using std::vector; etc.

Remove unused namespaces across a whole project or solution at once

Do you mean using statements? First, note that they generally do no harm other that take space.
Tools like ReSharper offer automated tricks to do this, however: there was a link in the VS feed a little while ago; it boils down to:

  • go to Tools -> Macros -> Macros IDE...
  • in the Project Explorer, Add -> Add Module... (put in a name - I've used OrganiseUsings)
  • paste over with the code below
  • File -> Save MyMacros, exit

Now if you right-click on the toolbar and Customize... - you should be able to find MyMacros.OrganiseUsings.RemoveAndSortAll - drag this somewhere handy (maybe the Tools menu; you might also want to change the name after placing it).

You can now use this option to run the Remove and Sort command for an entire solution. A big time-saver.

==== code ====

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module OrganiseUsings

Public Sub RemoveAndSortAll()
On Error Resume Next
Dim sol As Solution = DTE.Solution

For i As Integer = 1 To sol.Projects.Count
Dim proj As Project = sol.Projects.Item(i)
For j As Integer = 1 To proj.ProjectItems.Count
RemoveAndSortSome(proj.ProjectItems.Item(j))
Next
Next
End Sub

Private Sub RemoveAndSortSome(ByVal projectItem As ProjectItem)
On Error Resume Next
If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
Dim window As Window = projectItem.Open(Constants.vsViewKindCode)

window.Activate()

projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")

window.Close(vsSaveChanges.vsSaveChangesYes)
End If
End If

For i As Integer = 1 To projectItem.ProjectItems.Count
RemoveAndSortSome(projectItem.ProjectItems.Item(i))
Next
End Sub

End Module

How to remove unused using namespaces

Yes you can right click on the page and from the menu select;

Organise Usings > Remove Unused Usings

Alternatively if you prefer shortcuts, please use;

Ctrl + R + G

I am using this all the time, to clean up code, and make code compilation quicker.

Or you can use PowerCommands to remove unused usings for entire projects

Disable Resharper's Adjust namespace for specific files

Unfortunately it's a long existing bug in R# that disable CheckNamespace isn't respected by the "Adjust namespaces" refactoring. As I know, there is no other way to exclude specific files from this refactoring.



Related Topics



Leave a reply



Submit