tags:

views:

114

answers:

6

Hi All,

I have code that uses namespaces, a few of them and I am having some confusion in my brain.

If I have something like:

#include <vector>

protected:
    vector<registeredObject> mRegistryList;

The compiler complains that vector has 'no type'

Can't I just do this:

#include <vector>

protected:
   std::vector<registeredObject> mRegistryList;

or do I need to do:

using namespace std;
#include <vector>

protected:
    std::vector<registeredObject> mRegistryList;

What is the proper method for using multiple namespaces, etc?

+1  A: 

Either of the second two possibilities will work. In case you care, there's also a third:

using std::vector;

protected:
    vector<registeredObject> mRegistryList;

Of these, the using namespace std; is generally advised against -- it drags in a large number of names, some of which you basically can't even know about, so it's easy for it to collide with all sorts of things in your own code. Most people use the explicitly qualified name, like: std::vector... version most of the time.

Jerry Coffin
+3  A: 

I am assuming the code above is inside a class. In that case you want #2. In fact, you should avoid #3 inside a header, which of course is where class definitions reside. Otherwise, when you #include that header file, you're going to be very upset with the resulting namespace collisions.

rlbond
+1  A: 

the compiler complains that vector has 'no type'

That is because there is no class called vector in global namespace. And since you have not specified any namespace while defining vector compiler is unable to find the class.

Your second option is fine. There you are telling the compiler that vecror class resides in std namespace.

In the third option std:: in the definition of mRegistryList is unnecessary as you have already included the namespace by using std; statement. I feel that using statement pollutes the namespace (especially when used in the header files), so I prefer the second option.

Naveen
+1  A: 

First thing, never put a using namespace inside a header file otherwise all files that include this one will have all types from the given namespace imported in the global namespace. This will cause a lot of confusion.

Second thing, in your last example, you wrote using namespace before the #include instead of after all includes.

Third thing, if you write using namespace std; you don't need to fully qualify your std::vector and can use vector.

Finally, my preference is the following:

  1. In a header file, always use fully qualified namespaces.
  2. In a source file, if you use only one or two types from the std namespace, you can use using std::vector; instead of using the full namespace.
  3. In a source file where you use a lot of types from std namespace, go ahaead with using namespace std;.
Marcel Gosselin
A: 

Either the using declaration or explicitly specifying the namespace when used will allow you to use it. The problem that I believe you're running into is the "protected" declaration. Access specifiers like "protected" are only used in the context of a class.

As to which is the best to use, the use of the global "using" declaration is generally ill-advised, since chances are you do not need everything in the "std" namespace and it will defeat the purpose of having a separate namespace in the first place, that being to reduce the chance of name collisions. Sometimes, a good middle ground can be single using declarations for those objects you will use from a namespace, such as "using std::vector;". That will not drag in all std objects, just that particular one. Even in that case, however, that could result in a collision if anything else in your code is called "vector". It's generally best to use the explicit specifications.

Todd
+1  A: 

Using directives have the form using namespace some_name;, and the guideline is you should not use them outside function scope. Using them in function scope (even when that function is defined in a header, such as being inline or a template) is always fine.

Using declarations have the form using something::something;, and the guideline is you should not use them at global scope in headers. Using them in your own namespace, in class definitions, or in function scope is always fine.

If you follow these guidelines, you'll never have accidental name ambiguity problems and won't write headers that break other code when included.

Roger Pate