Using std Namespace

2018-12-31 07:21发布

There seem to be different views on using 'using' with respect to the std namespace.

Some say use ' using namespace std', other say don't but rather prefix std functions that are to be used with ' std::' whilst others say use something like this:

using std::string;
using std::cout;
using std::cin;
using std::endl;
using std::vector;

for all the std functions that are to be used.

What are the pros and cons of each?

15条回答
余欢
2楼-- · 2018-12-31 07:35

using namespace std imports the content of the std namespace in the current one. Thus, the advantage is that you won't have to type std:: in front of all functions of that namespace. However, it may happen that you have different namespaces that have functions of the same name. Thus, you may end not calling the one you want.

Specifying manually which ones you want to import in std prevents that from happening, but may result in a long list of using at the beginning of your file, which some developer will find ugly ;) !

Personally, I prefer specifying the namespace each time I use use a function, except when the namespace is too long, in which case I put some using at the beginning of the file.

EDIT: as noted in another answer, you should never put a using namespace in a header file, as it will propagage to all files including this header and thus may produce unwanted behavior.

EDIT2: corrected my answer, thanks to Charles comment.

查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 07:36

If you don't have a risk of name conflicts in your code with std and other libraries you can use :

using namespace std;

But if you want know precisely the dependancy of your code for documentation or there is a risk of name conflicts use the other way :

using std::string;
using std::cout;

The third solution, don't use these solutions and write std:: before each use in code brings you more security but, maybe a little heaviness in the code...

查看更多
时光乱了年华
4楼-- · 2018-12-31 07:37

What are the pros and cons of each

The only reason to leave off the std:: is that you could, in theory, reimplement all the STL functions yourself. Then your functions could be switched from using std::vector to my::vector without changing the code.

查看更多
裙下三千臣
5楼-- · 2018-12-31 07:40

Much like in Java where you can use either can include java.util.* or simply select each class individually, it depends on style. Note that you don't want one using namespace std at the start of your file/wide scope because you will pollute the namespace and possibly have clashes, defeating the point of namespaces. But if you have a function that uses a lot of STL, it clutters the code to have a jumble of prefixing syntax in your logic and you should probably consider using either using namespace std (when using a variety of classes) or individual usings (when using a few classes often).

查看更多
路过你的时光
6楼-- · 2018-12-31 07:41

Excluding the basics (Having to add std:: infront of all stl objects/functions and less chance of conflict if you don't have 'using namespace std')

It is also worth noting that you should never put

using namespace std

In a header file, as it can propagate to all files that include that header file, even if they don't want to use that namespace.

In some cases it is very beneficial to use things like

using std::swap

As if there is a specialized version of swap, the compiler will use that, otherwise it will fall back on std::swap

If you call std::swap, you always use the basic version, which will not call the optimized version (if it exists).

查看更多
公子世无双
7楼-- · 2018-12-31 07:42

Why not for example

typedef std::vector<int> ints_t;
ints_t ints1;
....
ints_t ints2;

instead of the unwieldy

std::vector<int> ints1;
...
std::vector<int> ints2;

I find that much more readable and its my standard for coding.

You can even use it to include some semantic information for the reader. For example, consider the function prototypes

void getHistorgram(std::vector<unsigned int>&, std::vector<unsigned int>&);

which ones the return value?

How about instead

typedef std::vector<unsigned int> values_t;
typedef std::vector<unsigned int> histogram_t;
...
void getHistogram(values_t&, histogram_t&); 
查看更多
登录 后发表回答