Creating a C++ namespace in header and source (cpp) [duplicate]
Asked Answered
R

9

103

Is there any difference between wrapping both header and cpp file contents in a namespace or wrapping just the header contents and then doing using namespace in the cpp file?

By difference I mean any sort performance penalty or slightly different semantics that can cause problems or anything I need to be aware of.

Example:

// header
namespace X
{
  class Foo
  {
  public:
    void TheFunc();
  };
}

// cpp
namespace X
{
  void Foo::TheFunc()
  {
    return;
  }
}

VS

// header
namespace X
{
  class Foo
  {
  public:
    void TheFunc();
  };
}

// cpp
using namespace X;
{
  void Foo::TheFunc()
  {
    return;
  }
} 

If there is no difference what is the preferred form and why?

Rochelle answered 21/11, 2011 at 11:19 Comment(0)
D
44

Namespace is just a way to mangle function signature so that they will not conflict. Some prefer the first way and other prefer the second version. Both versions do not have any effect on compile time performance. Note that namespaces are just a compile time entity.

The only problem that arises with using namespace is when we have same nested namespace names (i.e) X::X::Foo. Doing that creates more confusion with or without using keyword.

Damselfish answered 21/11, 2011 at 11:27 Comment(0)
D
60

The difference in "namespace X" to "using namespace X" is in the first one any new declarations will be under the name space while in the second one it won't.

In your example there are no new declaration - so no difference hence no preferred way.

Dalenedalenna answered 21/11, 2011 at 11:23 Comment(1)
It depends on the project and style. Often there's one main namespace for a load of files in a module, and the second style makes sense.Celindaceline
D
44

Namespace is just a way to mangle function signature so that they will not conflict. Some prefer the first way and other prefer the second version. Both versions do not have any effect on compile time performance. Note that namespaces are just a compile time entity.

The only problem that arises with using namespace is when we have same nested namespace names (i.e) X::X::Foo. Doing that creates more confusion with or without using keyword.

Damselfish answered 21/11, 2011 at 11:27 Comment(0)
T
9

There's no performance penalties, since the resulting could would be the same, but putting your Foo into namespace implicitly introduces ambiguity in case you have Foos in different namespaces. You can get your code fubar, indeed. I'd recommend avoiding using using for this purpose.

And you have a stray { after using namespace ;-)

Teddy answered 21/11, 2011 at 11:26 Comment(2)
I wouldn't call it stray, as it matches the closing } at the very end. However, I'd call that pair of braces redundant ;)Showdown
@blubberdiblub, the question was edited, if you checked the original version, you would call it stray ;-)Teddy
R
3

If you're attempting to use variables from one to the other, then I'd recommend externalizing them, then initializing them in the source file like so:

// [.hh]
namespace example
{
   extern int a, b, c;
}
// [.cc]
// Include your header, then init the vars:
namespace example
{
   int a, b, c;
}
// Then in the function below, you can init them as what you want: 
void reference
{
    example::a = 0;
}
Remise answered 15/6, 2020 at 2:57 Comment(0)
T
1

If the second one compiles as well, there should be no differences. Namespaces are processed in compile-time and should not affect the runtime actions.

But for design issues, second is horrible. Even if it compiles (not sure), it makes no sense at all.

Titanomachy answered 21/11, 2011 at 11:23 Comment(2)
I don't think it compiles, but not because there's a difference, but because there's a stray { ;-)Teddy
The difference is Foo::TheFunc() is declared in global namespace, while it is defined in the namespace X.Towny
T
1

The Foo::TheFunc() is not in the correct namespacein the VS-case. Use 'void X::Foo::TheFunc() {}' to implement the function in the correct namespace (X).

Towny answered 21/11, 2011 at 11:24 Comment(1)
The question's a bit old, but do you know what the consequences of this are? i.e. will you run into any problems with the way his VS case declares the functions in the namespace, but defines them outside of it?Tc
V
1

In case if you do wrap only the .h content you have to write using namespace ... in cpp file otherwise you every time working on the valid namespace. Normally you wrap both .cpp and .h files otherwise you are in risk to use objects from another namespace which may generate a lot of problems.

Verism answered 21/11, 2011 at 11:27 Comment(0)
J
1

Or you can do the following:

// asdf.h
namespace X
{
  class Foo
  {
  public:
    void TheFunc();
  };
}

Then

// asdf.cpp
#include "asdf.h"

void X::Foo::TheFunc()
{
  return;
}
Janeanjaneczka answered 4/11, 2021 at 10:29 Comment(0)
U
0

I think right thing to do here is to use namespace for scoping.

namespace catagory
{
    enum status
    {
      none,
      active,
      paused
    }
};

void func()
{
    catagory::status status;
    status = category::active;
}
Uranalysis answered 16/9, 2018 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.