-1

I can understand why we should have this line in a code. That way, you don't have to write std::cout or std::cin, etc.

For std::string though, does the compiler get confused if I include in a c++ code? For variable str below, is it considered as a cstring type of string or a std::string?

include <cstring>
include <string>

using namespace std;

string str;  

In other words, if I have "using namespace std" at the beginning of the code, is it safe to write all "std::string" simply as "string"? Also If I have "using namespace std", I do not need "using std::string", right?

ddd
  • 4,665
  • 14
  • 69
  • 125
  • 1
    It is an `std::string` (assuming you have `#include`.) But `using namespace std;` is a terrible idea in most cases. – juanchopanza Feb 27 '14 at 17:06
  • C strings are character arrays or pointers. No conflict at all. – chris Feb 27 '14 at 17:10
  • 3
    "you don't have to write std::cout or std::cin, etc." -> actually, you will find that many experts do write their code like this. Code is more often read than written, so you should aim for easy reading, not easy writing. – Christian Hackl Feb 27 '14 at 17:14
  • 1
    The `` or `` header does not define anything called `string`, so there's no conflict. – Keith Thompson Feb 27 '14 at 17:17
  • People who write `std::cout` `std::cin` etc are just wrong. It's wrong because it needlessly clutters up the language. Obviously using `using namespace std;` is optimal (and obviously not in a header file) – Brandin Feb 27 '14 at 19:40
  • @Brandin What's the point of namespaces if you subvert them via `using namespace`? – fredoverflow Feb 27 '14 at 19:45
  • @FredOverflow What's the point of namespaces if you have to type `foo::do_something` all the time? With this convention why not just do it the C way and name everything uniquely and just always write `mylib_do_something(...)` for function calls, class names, etc – Brandin Feb 27 '14 at 19:53
  • 1
    @Brandin You only need to qualify with `foo::` if you're crossing namespace boundaries. And personally, I find C++ code with explicit `std::` qualifications a lot more *readable* than without them. – fredoverflow Feb 27 '14 at 20:01

1 Answers1

5

There is no "cstring type of string". The <cstring> header contains no string class, only functions (and the size_t type and the NULL macro). In your example, string would just be considered std::string.

As for using namespace. I usually just use it in a very small scope, such as inside of function bodies. Never in header files!

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62