3

Is there any functional or optimization difference between the following?

// SomeClass.cpp
#include "SomeClass.h"
#include "SomeOtherClassInSomeClassNamespace.h"

using namespace SomeClassNamespace;

SomeClass::SomeClass() {}

void SomeClass::SomeFunc() 
{
    // uses some stuff in SomeClassNamespace not defined  in SomeClass.h
}

or

// SomeClass.cpp
#include "SomeClass.h"
#include "SomeOtherClassInSomeClassNamespace.h"

namespace SomeClassNamespace
{
    SomeClass::SomeClass() {}

    void SomeClass::SomeFunc() 
    {
        // uses some stuff in SomeClassNamespace not defined  in SomeClass.h
    }
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
David
  • 27,652
  • 18
  • 89
  • 138
  • @Bala R not quite - I know the possible perils of the using directive but this is specific case not talked about there – David Oct 17 '11 at 03:49

3 Answers3

2

Is there any functional difference between the following?

As long as the merged scopes resolve as expected, then No.

Is there any optimization difference between the following?

Runtime or binary size? No.

If you want build optimizations, then additional complexity will be introduced via using and/or by reopening the namespace.

I don't use the former because resolutions can be problematic.

I don't use the latter because it's easy to end up with new declarations.

The way I use it:

namespace {
  // ... private stuff
}

SomeClassNamespace::SomeClass::SomeClass() {}
...

this is a little verbose, but fast to resolve definitions with their declarations, reduces chance of collisions, can reduce programmer errors, and could reduce binary size if the definitions in the anonymous namespace ends up being exported (in case you also use private implementations). Of course, it will also make sense to keep your internals ("some stuff in SomeClassNamespace not defined in SomeClass.h") in the right namespace (assuming they are used in multiple TUs).

justin
  • 104,054
  • 14
  • 179
  • 226
  • Yeah I use unnamed namespaces along with either case. Another possibility which sometimes suits my needs is the using declaration instead. `using SomeClassNamespace::SomeClass;` That way I dont need to prefix every definition with `SomeClassNamespace::`. The annoyance is when I have 5 or 6 classes in a _system_ all in a namespace, I don't want to write a bunch of using declarations at the top of each cpp. – David Oct 17 '11 at 12:43
  • @Dave maybe you want to try namespace aliases `namespace SMClsNs = SomeClassNamespace;` or typedefs in your internals? fully qualified scopes have not bothered me for some time... – justin Oct 17 '11 at 18:07
  • They don't bother me in user code, they do bother me in implementation of a class _in_ that namespace – David Oct 17 '11 at 18:34
1

This is the only time where I ever use using namespace X;

And I make sure that the only thing in the file are the definitions of the classes inside the namespace X (and only one class at a time).

The only reason I do it this way is because (and a personal opinion) I think it makes it easier to read overall. And it documents the namespace we are working in one line at the top (especially if it is a multilevel nested namespace).

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Out of curiosity, what do you do when the class is in a nested namespace? `using namespace` both namespaces or just the bottom-most one? – David Dec 01 '15 at 16:49
  • @David: Have a look at my code: https://github.com/Loki-Astari/ThorsSerializer/tree/master/src/Serialize – Martin York Dec 01 '15 at 17:53
0

With namespace { to be consistent you should indent everything in the file which will reduce the amount of actual code you can put on a line before it scrolls out of view.

You don't have this problem with a using statement.

You may also get more confusing error message with the namespace { option because the closing brace for the namespace may close something else which wasn't properly closed. You may end up with confusing messages about the namespace missing a closing brace.

Other then that, I'm unaware of any difference.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
  • Proper indenting becomes _problematic_ when you have a few subnamespaces. Alot of people special-case indentation for this (by leaving it flat). – David Oct 17 '11 at 04:06
  • @Dave, yes but that inconsistency bothers me. Seems slightly better to use `using namespace`. Really minor, but since there is almost no difference between the two forms... – Winston Ewert Oct 17 '11 at 04:09