quant_dev
quant_dev

Reputation: 6231

Should I use std:: and boost:: prefixes everywhere?

In my C++ code I don't use the declarations using namespace std; or using namespace boost;. This makes my code longer and means more typing. I was thinking about starting to use the "using" declarations, but I remember some people arguing against that. What is the recommended practice? std and boost are so common there should be no much harm in that?

Upvotes: 7

Views: 2169

Answers (7)

Alexandre C.
Alexandre C.

Reputation: 56956

I use using namespace only inside function bodies. In header files, I always qualify the namespace explicitly.

Seldom (when copy pasting a colleague's code for mockup), I use using namespace at namespace scope (ie. for the whole translation unit).

Upvotes: 0

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247929

One factor to keep in mind is that the std namespace is named this way to make it short. A std:: prefix is only 5 characters, hardly the end of the world. That's unlike .NET's namespaces like System.Collections.Generic. It is designed to be easy to type.

For that reason, I usually just type out the std prefix. Boost isn't too bad either, so I usually type that out too.

I usually alias the sub-namespaces (boost::filesystem for example) to something shorter (namespace fs = boost::filesystem for example)

Using typedefs liberally helps too. And if I need to reference a type often, I might just add a using for it.

But I generally try to avoid using's in headers especially, and when I do use them, I prefer to put them at function scope to avoid polluting the actual namespace.

C++ offers a lot of tools that let you avoid having to specify the namespace, without polluting the global namespace.

Upvotes: 4

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95489

In header files, yes. That is because using "using std::name_of_std_member;" or using "using namespace std;" in a header file will cause all other files which include that header file to see the symbol in the global scope, thus defeating the purpose of namespaces. In source files, however, it is perfectly ok to use "using namespace std;" to make the symbols of that namespace available with the "std::" prefix.

Upvotes: 1

anon
anon

Reputation:

My own rules are:

  • in header files, all names are explicitly qualified e.g. std::string, std::cout etc. at their point of use
  • in source files, place using clauses for the commonly used names at the top of the file eg. using std::string;
  • never use using namespace xxxx; in production code.

Upvotes: 7

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99565

I use using namespace only in C++ files, not in headers. Besides, using hole namespace not needed in most of times. For instance, you could write using boost::shared_ptr or using std::tr1::shared_ptr to easily switch between shared_ptr implementations.

Sample:

#include <iostream>

using std::cout;

int main()
{
    cout << "test" << std::endl;
    return 0;
}

Upvotes: 16

xtofl
xtofl

Reputation: 41509

The fact that the code is cluttered with ::std:: prefixes is indeed annoying when reading the code. However, you want to know what namespace a symbol was in as easily as possible...

Now isn't that the IDE's job?

As long as my IDE doesn't support 'view short typenames', I'm leaning towards using declarations for commonly known symbols (i.e. the STL, boost, ...). Readibility first!

Upvotes: 5

ypnos
ypnos

Reputation: 52337

Using namespace ... was not invented just for fun.

If you have a good reason to use it, then do so (and the frequent use of stuff from these namespaces is the exact good reason). Don't listen to fanatics who tell you everything they don't want to do for obscure reasons themselves is evil.

However, a good source for reasoning in these regards is C++ FAQ lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

I have read it and still decided to use it like you want to. Now you can make your own informed decision :-)

Upvotes: 16

Related Questions