5

What is using namespace std; in recent C++?

In old compilers such as Turbo C++, this appears to be unsupported as it causes a compiler error. In more recent C++ compilers, this is the only method for compiling and running the program.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
darla_sud
  • 359
  • 1
  • 4
  • 15
  • 2
    It is [something that should be avoided](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – juanchopanza Oct 27 '13 at 12:49
  • 7
    What you call "recent" is 15 years old, and what you call "old" is truly ancient and best forgotten – Jonathan Wakely Oct 27 '13 at 12:58
  • 1
    Please use more descriptive title in the future, and if possible spend some more time writing it to correct simple errors and typos. – user2802841 Oct 27 '13 at 13:01
  • 1
    _In more recent C++ compilers, this is the only method for compiling and running the program._ -- No, it isn't. Prepending `std::` to items that live in that namespace is another method for compiling your program (at least, in any compiler you want to be using). – mah Oct 27 '13 at 13:15
  • If u can tell me the better compilers for C++..for old programs...without "using namespace.std;" – darla_sud Oct 27 '13 at 13:31

3 Answers3

6

Turbo C++ is a 20+ year old compiler. You shouldn't be using it.

That said,

#include <iostream>
using namespace std;

in a modern compiler is the same as writing the following in Turbo C for the standard headers.

#include <iostream.h>

Turbo C++ is pre-namespaces. So all standard headers are not in namespace std. So you don't need the using namespace std.

You will never need using namespace std in Turbo C++ because it doesn't support namespaces. Turbo C++ doesn't support probably 50% of C++ stuff - it's that old. Use Visual C++ (Express editions are free) or G++ for C++.

user93353
  • 13,733
  • 8
  • 60
  • 122
  • 1
    thank you very much @user93353 but in our college faculty saying that Turbo is the best one for basic compiling..thats okay but while searching programs in the internet i am getting new ones only..those programs are not working in Turbo c++. What is the alternate? – darla_sud Oct 27 '13 at 12:54
  • 5
    See http://isocpp.org/get-started for some freely-available modern compilers – Jonathan Wakely Oct 27 '13 at 13:02
  • 4
    You need to find a new college. You're obviously not going to get taught real C++. – Crowman Oct 27 '13 at 13:15
  • 1
    Thanks but now its not possible to change the college...if u can tell me the better compilers for C++..for old programs...without "using namespace.std;" – darla_sud Oct 27 '13 at 13:20
  • 3
    There's no point in learning obsolete C++. If you want to learn basics, look into Assembler and/or C - if you want to learn proper C++, get a modern compiler. – Frank Osterfeld Oct 27 '13 at 13:40
6

C++ uses "namespaces" to group related classes and functions. The C++ Standard Library is almost entirely defined inside a namespace called std (short for "standard"). When you #include a standard header such as <string> it contains definitions like this:

namespace std
{
  template<typename T>
    class allocator;

  template<typename Ch>
    class char_traits
    {
       // ...
    };

  template<typename Ch, typename Traits = char_traits<Ch>, typename Alloc = allcoator<Ch>>
    class basic_string
    {
       // ...
    };

  typedef basic_string<char, char_traits<char>, allocator<char> > string;
}

The names allocator, char_traits, basic_string and string are all declared in namespace std, so after including that header you need to refer to them as std::string etc.

Alternatively, you can use a using-directive such as using namespace std which makes all the names from namespace std available in the current scope, so following the using-directive you can just say string instead of std::string.

The ancient TurboC++ compiler does not follow the standard, so its standard library just puts names in the global namespace, so you have to refer to string not std::string and you can't use using-directives. The first C++ standard was published in 1998, so you should not be using pre-standard compilers in 2013, it will not be a valuable education.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
2

using namespace std; tells the compiler that you don't care about the carefully designed use of std in the standard library, but would rather deal with unpredictable and sometimes disastrous name conflicts on your own.

Don't use it. It is never necessary, even though some IDEs insert it automatically. If you have one of those, delete the declaration whenever it occurs.

To use names from the standard library, qualify then with std::. Like this:

#include <iostream>

int main() {
    std::cout << "Hello, world\n";
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165