views:

285

answers:

11

What is generally considered good coding style in C++ where you use types from the standard library? For example, if I have a using namespace std; directive would you still expect to see library types fully qualified like so: std::string or is it acceptable to just use string as the type identifier?

If you do fully qualify, can you expain the rationale behind it?

+11  A: 

I'd prefer using:

using std::string;
string whatever;

than fully bringing the namespace in.

In any case library developers should avoid type names that conflict with the standard ones although string probably is quite common.

For libraries other than standard I like to qualify if the nesting namespaces is not too long, if it is just typedef it to a meaningful name that includes the library name or something like that.

Arkaitz Jimenez
The whole point of namespace is that you don't have to worry about the names used by other libraries, even the standard ones.
anon
Choosing names that are already defined in the standard for your own library is a really bad idea. Not ilegal, just a bad idea as anyone that reads vector will think on std::vector.
Arkaitz Jimenez
+1  A: 

Generally I will fully qualify namespaces instead of using using namespace x mostly when I deal with header files. Source files may not want to have that namespace assumed and you shouldn't force it on the source files that include your header file.

I do both using namespace x and fully qualify namespaces depending on a personal judgement call about if I will re-use the type multiple times or not in source files.

Brian R. Bondy
damn, beat me to the punch by 42 seconds
sdellysse
I think this is wrong. You should NEVER have a using x; in any header file. If these mythical users don't want to have to provide suitable using directories, tough luck for them.
anon
@Neil Butterworth: I agree, and I modified the statement to be more forceful.
Brian R. Bondy
@Neil:I agree completely for a using directive (`using namespace x`). It's a whole different story, however, for a using declaration inside a limited scope (e.g. `using std::string` inside a class that uses string a lot). The former "leaks" all the names to the code where it's included, but the latter does not.
Jerry Coffin
@Jerry Good point. But I think I would habe to use the name A LOT before I'd switch to this. So far, I've never encountered such asituation, and I suspect that any class where it was necessary would be rather badly designed.
anon
+10  A: 

fully qualify in header files. import the namespace in the .cpp files.

keeps the global namespace from being cluttered by a simple #include

sdellysse
It also prevents you from forcing the namespace on someone if they decide to ever include your header file. It can create some confusion if they're wondering "where in god's name did I use that namespace?" and they don't think to look at someone else's code they've included.
Jesse O'Brien
280Z28
+1  A: 

namespace is basically introduced to minimize conflicts symbol names such as function, class, and variables. It is okay that you just use string than std::string unless your own library has string in its own space. I'm virtually not using very common namespace like std.

minjang
A: 

Totally subjective question: I would say acceptable to just use string. IDE/compiler are clever enough to find out what you mean. If you have objects with the same name, for example 2 string types. Then you have a different problem because the compiler won't know what you mean AND the coder won't know what you mean.

Other reason to not use library names is because of cluttering the code: In C# system.xml.xmltextreader is just overkill. XmlTextReader is enough to under stand what it is. Where it's located is almost never the issue

PoweRoy
The IDE may be "clever enough", but the compiler won't be.
anon
Why won't the compiler be clever to now that string is the same as std::string if you use 'using namespace std'. compiler won't be clever if you have multiple types with the same name, see 2nd line.
PoweRoy
+2  A: 

I do tend to adhere to two rules:

  • In header files, you want to qualify type names with the full namespace and never, ever want to put something like using namespace std; as this can/will cause interesting problems due to unexpected naming conflicts that you will need to track down at 1am.
  • In implementation files, I do tend to pull in symbols I use from other namespaces using using std::string; or similar. Actually, I'm not a 100% consistent with this as I often don't pull in the std namespace, but do pull in the project namespaces but that's personal preference. Never, ever put using namespace somethingorother; above any #include, though.
Timo Geusch
I use these rules with one addition: the largest allowable scope for a "using" clause in an implementation file is a function.
ceretullis
That's not a bad idea; I've started using that but I make it dependent on the overall usage. For example, using std::string at function scope can get a bit tedious so that tends to get moved out to file scope instead.
Timo Geusch
A: 

In general, i'd prefer (1) to using directive like using std::string; string hello; as what Jimenez noted previously; (2) i also use a anonymous namespace, subsequently use the using namespace directive to import all names into my anonymous namespace like this namespace { using namespace std; // or using std::string string blah_blah_blah; }

Raymond Tay
Just for what it's worth, a `using directive` is one like `using namespace x`. One like `using std::string` is a `using declaration`.
Jerry Coffin
Thanks Jerry, appreciate that correction.
Raymond Tay
+3  A: 

Just for what it's worth, there are a few things you can do by pulling a namespace with a using directive that you can't do by qualifying names. The canonical example is probably writing a generic sort function. If a swap has been defined for the type being sorted, you want to use that, but if it doesn't have a swap of its own, you want to use std::swap.

To accomplish this, you can write your code like:

using namespace std;
// ...
template <class T>
void my_sort(std::vector<T> &x) {
    // ...
    if (x[j] < x[k])
        swap(x[j], x[k]);

Now, if there's a swap in the namespace of whatever type is being sorted, it'll be found by argument dependent lookup. If there's not, std::swap will be found because you've made it visible with the using directive.

Jerry Coffin
+5  A: 

The whole point should be to avoid confusion and generate clear code. Omitting namespaces can in some cases obfuscate the origin of a function or class, but adding always full namespaces can become unbearable.

  • Avoid using namespace in header files
  • Use using namespace in source for "obvious" libraries (e.g. std, or the library to test in a test program)
  • You can alias namespaces in your source to keep it short and readable:

Example

namespace fs = boost::filesystem;
bool fileExists = fs::exists(fs::path(filePath));

EDIT, for completeness: using namespace in header files pollutes source files with imported namepaces in a non-obvious way ('nuf explanations of this already in this thread).

catchmeifyoutry
A: 

I always fully qualify within headers. I never put "using..." statements in headers.

My preference is to fully qualify in implementation files as well, however if the organization standard is to incorporate "using..." statements within the implementation files I will do so,

Liz Albin
A: 

If you do not fully qualify, you will have great hassle when moving inline methods from headers to CPP files or vice versa, and you definitely want one style across your projects.

Otherwise it's no problem, sometimes I qualify despite existing using statement. In headers it is a problem, Unless it is within scope:

// somefile.h
namespace VisionMap
{
     namespace X
     {
         using namespace Y;   // Does not impact the header's user, 
                              // as would if it was in the global scope.
     }
}
Pavel Radzivilovsky