Namespace (C++)

A namespace groups related names (functions, classes, variables) under a label to prevent naming collisions. The standard library lives in the std namespace, which is why you write std::cout.

C++

You access a name inside a namespace with the scope resolution operator ::, as in std::string. Writing `using namespace std;` lets you drop the std:: prefix, but it is often avoided in larger projects because it can cause ambiguous names.

You can also define your own namespaces to organize a large codebase.

Syntax

namespace name { /* declarations */ }

Examples

C++

#include <iostream>
using namespace std;

int main() {
    cout << "No std:: needed here" << endl;
    return 0;
}

Output:

No std:: needed here

Learn it in a lesson

See also

Back to the Dictionary