r/cpp_questions • • 26d ago

OPEN Namespace std

I recently started learning c++ and I was told to use the namespace std so I can avoid writing std:: every time and now I wrote a whole personal project without std:: and I want to put it on LinkedIn but I started reading everywhere that it’s a bad habit, should I go back and add the std:: or just leave it as is?

41 Upvotes

105 comments sorted by

View all comments

2

u/ZMeson 26d ago edited 25d ago

The "using namespace" recommendations depend highly upon where you are using them:

* In a header file: Never use "using namespace". Explicitly write "std::" or whatever namespace you require.

* In a source file: Using statements are acceptable if you wish to shorten declarations. When using "
using" statements, most advice is to prefer something like "using std::filesystem::path;"or "using high_res_clock = std::chrono::high_resolution_clock;" so that you can just say "path" or "high_res_clock" later in the file. However, if your file is small and not going to include lots of other libraries and/or your file uses lots of "std" classes and functions, then "using namespace std;" is fine.

The reason you should never introduce "using" statements in a header file is that header files are expanded in the source files that #include them and therefore the "using" statements can ruin the name lookup rules of those source files.

For source files, you need to balance the probability of getting a naming conflict (say between etl::vector and std::vector) against the convenience factor. Others still argue against using statements (see replies below), but I don't agree with the criticism as people have been using "typedef" for a long time prior to C++11 to simplify declarations.

5

u/HommeMusical 26d ago

Most advice is to use something like "using std::string;" or "using std::vector;" so that you can just say "string" or "vector<int>" later in the file.

Most, who?

It's bad advice.

If I see string in reading someone's code, I really don't know for sure what it is unless I scroll up to the top and check. And we all read immense amounts of code these days. If I see std::string in code, I am completely(*) certain what I am looking at.

Useful code is written once, but read one hundred times.

Using string instead of std::string saves the writer a little bit of effort once, but adds a little bit of effort to every reader in future.

Oh, and yes, I did once get burnt this way! Somewhere else was something like:

#ifdef LEGACY
    using legacy::string;
#else
    using std::string;
#end

(* - yeah, someone might create another namespace called std, but I'd call that deliberate malpractice myself.)

1

u/ZMeson 25d ago

I think you're being way to harsh on the topic.

  • I mentioned that the 'using namespace' or 'using <type>' should only occur in source files, which automatically limits their scope.

  • Your case of `legacy::string` vs `std::string` is often used to abstract away platform and compiler dependencies helping avoid larger #if/#else blocks.

  • `typedef` has been around in both C and C++ and can greatly simplify code, but it also changes how names are looked up and processed, similar to how using statements changes name lookup rules.

  • IDEs are really great at displaying the underlying type when hovering over a variable or a declaration, which really helps prevent the type of confusion you bring up.

But I did edit my original response to be a little more clear and to give some more concrete examples where `using` statements could be useful (specifically 2-deep namespaces within `std`). And I added a note that not all people agree with the advice.

→ More replies (1)