Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!

including namespace(s)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 proanim   Members   -  Reputation: 449


Posted 13 January 2013 - 04:53 PM

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



#ifndef _BASE_H_
#define _BASE_H_

// included headers

// rest of declarations


// namespaces
using namespace glm;
using namespace std;




#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?


#2 rip-off   Moderators   -  Reputation: 9544


Posted 13 January 2013 - 05:31 PM

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.

#3 Dan Mayor   Crossbones+   -  Reputation: 1715


Posted 13 January 2013 - 05:52 PM

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.

Digivance Game Studios Founder:

Dan Mayor - Dan@Digivance.com

#4 Servant of the Lord   Crossbones+   -  Reputation: 24348


Posted 13 January 2013 - 06:01 PM

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)

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]

#5 SiCrane   Moderators   -  Reputation: 10366


Posted 14 January 2013 - 09:18 AM

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

#6 Ravyne   GDNet+   -  Reputation: 10552


Posted 14 January 2013 - 10:00 AM

Its best to be explicit.


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

throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");

#7 wintertime   Crossbones+   -  Reputation: 2633


Posted 14 January 2013 - 10:26 AM

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.

#8 Llamaiyama   Members   -  Reputation: 133


Posted 16 January 2013 - 09:14 PM

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


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.