i want correct usage of it

Recommended Answers

All 3 Replies

http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html#SECTION00044000000000000000

That link is really opinionated. using namespace std; says that every name in the std namespace is available without having to prefix it with std:: . using std::{name}; says that only the specified name is available without a prefix. Without either of those, you have to prefix every instance of every name in the namespace with std:: .

There's no 'correct' usage. The most flexible choice is prefixing everything with std:: . That way you cherry pick which names to use and there's no chance of clashes. But it's very verbose and lines get long really fast.

For a few names used a lot, using std::{name}; is a good way to tame the verbosity of the prefix method. You know what names are prefixed and this can be put in the global scope without worrying about it:

#include <iostream>
#include <vector>

using std::cout;
using std::vector;

int main()
{
    vector<int> v;
    int input;

    cout << "Type some numbers: ";

    while (std::cin >> input) v.push_back(input);

    vector<int>::const_iterator x = v.begin();
    vector<int>::const_iterator y = v.end();

    while (x != y) cout << *x++ << '\n';
}

A better practice is putting the using std::{name}; at the top of the block you use it in. That way you still get the verbosity savings and also make accidental clashes easier to find and fix. Anything in the global scope still gets a prefix:

#include <iostream>
#include <vector>

template <class Container>
void PrintContainer(Container container)
{
    using std::cout;

    Container::const_iterator x = container.begin();
    Container::const_iterator y = container.end();

    while (x != y) cout << *x++ << '\n';
}

int main()
{
    using std::cin;
    using std::cout;
    using std::vector;

    vector<int> v;
    int input;

    cout << "Type some numbers: ";

    while (cin >> input) v.push_back(input);

    PrintContainer(v);
}

The best practice for using namespace std; is the same for using std::{name}; . Limit the scope to at least the namespace or function level. This avoids the verbosity of prefixing names and also minimizes the chance of name clashes and makes fixing accidental clashes easier:

#include <iostream>
#include <vector>

template <class Container>
void PrintContainer(Container container)
{
    using namespace std;

    Container::const_iterator x = container.begin();
    Container::const_iterator y = container.end();

    while (x != y) cout << *x++ << '\n';
}

int main()
{
    using namespace std;

    vector<int> v;
    int input;

    cout << "Type some numbers: ";

    while (cin >> input) v.push_back(input);

    PrintContainer(v);
}

It doesn't give you control over which names are available though. If you write your own min function, include <algorithm>, and do a using namespace std; , you still have to do something to resolve the ambiguity:

#include <algorithm>
#include <iostream>

template <class T>
const T& min(const T& lhs, const T& rhs)
{
    using namespace std;

    cout << lhs << '\t' << rhs << '\n';

    return (lhs < rhs) ? lhs : rhs;
}

int main()
{
    using namespace std;

    int lhs, rhs;

    cout << "Type two numbers: ";

    if (cin >> lhs >> rhs)
    {
        // resolve to namespace std
        cout << std::min(lhs, rhs) << " is smaller\n";

        // resolve to global namespace
        cout << ::min(lhs, rhs) << " is smaller\n";
    }
}

The worst choice in theory is putting using namespace std; in the global namespace:

#include <algorithm>
#include <iostream>

using namespace std;

template <class T>
const T& min(const T& lhs, const T& rhs)
{
    cout << lhs << '\t' << rhs << '\n';

    return (lhs < rhs) ? lhs : rhs;
}

int main()
{
    int lhs, rhs;

    cout << "Type two numbers: ";

    if (cin >> lhs >> rhs)
    {
        // resolve to namespace std
        cout << std::min(lhs, rhs) << " is smaller\n";

        // resolve to global namespace
        cout << ::min(lhs, rhs) << " is smaller\n";
    }
}

It increases the chance of a clash somewhere because all names in the namespace are available without a prefix and a lot of names in namespace std are very common, like min and max, or find and search. But if you get a clash, the code won't compile, so it's not the end of the world. A global using namespace std; makes it a little harder to find and fix. A good IDE removes that hardness.

I think the best choice is picking your most common names with using std::{name}; and prefixing everything else. But most of my actual code will have using namespace std; in a limited scope or globally for toy programs. It's easier and I know the names in the std namespace and can avoid clashes by not redeclaring them with the same signature.

As long as you know the tradeoffs for whichever one you choose, they're all 'correct'. Pick one style, use it consistently, and keep an open mind in case somebody tries to convince you that their way is better. :)

commented: Solid advice IMO +19
commented: great explanation :) +36
commented: Excellent! +10
commented: Well explained +14
commented: Good post +5
commented: Awesome explanation! Thanks! +2

Tom explained it very well but just a few more things:
If you don't already know what a namespace is: it is defined with namespace <name>. It's used to group certain things that are similar to each other together. For instance:

namespace sock
{
    void Init()
    { // initialize winsock here
    }
    class sock_class
    { // sock_class's code goes here
    }
    // ext...
}

later in your code.. to use this you must do sock::Init() to initialize winsock and sock::sock_class to access the class. OR you can include:
using namespace sock;
This will allow you to do it with out the sock:: so: Init(), or sock_class.

The Global Namespace is basically the unnamed namespace that you code in. It is invisable so technicality in your code you could include :: before everything, but that would just be annoying -.-.

I hope that sums up everything about why you use "using namespace std;"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.