# including namespace(s)

This topic is 1831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am looking for the way to use namespaces more efficiently. At this point I use 'glm' and 'std' namespace, and I have found that it cannot be included from one header to the rest like this

base.h

#ifndef _BASE_H_
#define _BASE_H_

...

...
// rest of declarations

...

// namespaces
using namespace glm;
using namespace std;

#endif

app.h

#include "base.h"

// rest of the code goes here

When I do this I get compiler error around namespaces, since I don't like things like 'glm::' and 'std::' in almost every line of code.

Is there a way to use namespaces so they are declared only in one header instead of every header that uses them?

##### Share on other sites

You can use individual pieces of the name spaces which can help to eliminate the confliction's.  Like if you are using std::string and glm::whatever, you could...  (May vary based on language, I'm more familiar with C#)

using std::string;

using glm::whatever;

Alternatively you can also "inline include" or scope into a namespace as needed which is the preferred practice I believe.  That is to say...  (Again this varies on your language and is NOT correct syntax.)

using namespace std

{

... stuff here

}

using namespace glm

{

... glm stuff here

}

Personally I believe it's a matter of preference.  I prefer to use individual objects (the first way) many other's prefer to inline scope or whatever you want to call it (the second way).  Anyway, point is compiler errors when using different namespaces probably means that both namespaces define an object with the same name.  So saying to use std and glm (maybe they both have a "string") the compiler doesn't know which "string" you want to use.  Either approach will get past the error, I believe which one you choose should be up to you, I don't know if there is an official standard or any particular difference between the two methods.

##### Share on other sites
Note that putting a using directive in a header can actually silently change the meaning of existing code. Let's say you're working with DirectX 9 and have the function with the signature void rotate(D3DVECTOR3 *, D3DVECTOR3 *, D3DVECTOR3 *); however, you want to use the operator overloads so all your vectors are actually D3DXVECTOR3 objects. This isn't a problem because D3DXVECTOR3 pointers have an implicit conversion to D3DVECTOR3 pointers. However, if you directly or indirectly include the algorithm header and someone has added a using namespace std; somewhere in a header all of the sudden your code will no longer call the version of rotate() it was using before, because std::rotate() is a template function that would provide a better match to (D3DXVECTOR3 *, D3DXVECTOR3 *, D3DXVECTOR3 *).

##### Share on other sites

Its best to be explicit.

Be ye a programmer, or be ye a lazy typist?

##### Share on other sites

The documentation of glm explicitly says you should not do a using namespace glm, because there are many short named things inside to make it more like GLSL.

If you really want a using namespace std do it explicitly in a cpp file where you need it, but in a header you should type everything out and not put a using to not give up all usefulness of namespaces, like the others said.

##### Share on other sites

In C++ you can also have namespace aliases if you aren't wanting to type really long namespaces.

namespace xyz = Some::Really::Long::Namespace;

xyz::Foo();