including namespace(s)

Started by
6 comments, last by Llamaiyama 11 years, 3 months ago

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_

// included headers
...


...
// 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?

Advertisement

The recommended practise is not to put "using" declarations in header files at all.

The only way around this that I can think of that might work would be to place all your standard includes and all your "glm" includes, followed by the using statements, in a giant (precompiled) header. Even still, whatever is causing them to conflict will probably surface when you try to use the ambiguous identifiers. You'd need to use this header instead of including any standard or "glm" header elsewhere. This has lots of disadvantages, particularly compile time might suffer depending on your project.

I'd recommend trying to live without "using" statements. Surprisingly soon you won't miss them much. I, like most beginners I suspect, started off by by relying on "using" declarations and couldn't understand why anyone would write the "long" and "ugly" way. Now I find it difficult not to "autocomplete" something like std::string, even when helping such aforementioned beginners here on these forums.

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.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

I like using the namespace directly, without any "using", unless it's multiple namespaces deep. So "std::string" instead of "string".

As has already been mentioned, it's considered bad practice to put "using" in a namespace, because it completely defeats the purpose of the namespace by bringing the entire namespace into the global scope. 'using' basically renamespaces a namespace or a specific function or class in a namespace. Namespaces have benefits to them, and if you "renamespace" the namespace into the global scope, you lose all the benefits.

(There are other uses for the 'using' keyword as well, but that's unrelated)

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 *).

Its best to be explicit.

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

throw table_exception("(? ???)? ? ???");

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.

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();

This topic is closed to new replies.

Advertisement