Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple namespaces [closed]

Tags:

c++

c++11

"I am brand new to C++, and fairly new to coding in general. I have a few questions regarding namespaces.

What exactly is a namespace, and is it possible to use multiple namespaces in a single source file?

I was also curious about the result of introducing a namespace in different locations in a source file.

I know it is possible to specify a namespace at the beginning of a source file, ie after including the input output stream,

using namespace std;

or within functions and within lines of code,

std::cout << … << std::endl;

What is the difference between introducing these two namespaces in these two locations?

Are there other ways to introduce a namespace to a source file?

like image 379
Davis Brown Avatar asked Aug 13 '26 06:08

Davis Brown


2 Answers

What exactly is a namespace, and is it possible to use multiple namespaces in a single source file?

A namespace is like a container for variable and function names. When you have a very large project, you might find that more than one part of the project may use the same name for something.

You can use as many namespaces as you'd like in a single source file.

int aVariable;

namespace ModuleA
{
    int aVariable;
}

namespace ModuleB
{
    int aVariable;
}

None of these declarations of aVariable interfere with each other because they are all in different namespaces.

I was also curious about the result of introducing a namespace in different locations in a source file.

There are different ways to use a specific namespace...

// Somewhere in your code...
int var = aVariable;              // Uses the global aVariable
int varA = ModuleA::aVariable;    // Uses the ModuleA aVariable
int varB = ModuleB::aVariable;    // Uses the ModuleB aVariable

You can also use the using keyword to import one or more of the variables or functions from a namespace into the current scope.

// Take everything from a namespace
using namespace ModuleA;
// Or take just one thing from a namespace
using ModuleB::aVariable;

There is a gotcha with the example I have given so far; aVariable can only be declared once in any context, so when you have both a global aVariable and use using namespace ModuleA; you will end up declaring the aVariable name twice in that context! This will show up as a compiler error.


One more important thing to know about using namespace ...:

If this line is found in a header file, any other file that includes that header will also receive the imported names from that namespace. This can lead to unexpected name collisions between your code and other people's code. It is generally frowned upon to put using namespace std; in any header file.

I recommend that you use the fully qualified name for a variable or function as much as possible. So use std::min(a, b) instead of just min(a, b). Even if you have a using namespace std; in your source code the std:: prefix still works. Eventually when your project gets large enough you may need to remove the using namespace std; line and then you won't have to fix hundreds of uses of std:: functions.

like image 185
Romen Avatar answered Aug 14 '26 20:08

Romen


One major difference is that every calls to a free function in a namespace becomes unqualified when using a namespace.

This is useful for enabling ADL (Argument Dependent Lookup) calls:

namespace A {
    struct S {};
    void swap(S&, S&) {
        std::cout << "swapped S" << std::endl;
    }
}

template<typename T>
void adl_swap(T& a, T& b) {
    using namespace std;

    // we don't tell std::, so it's an unqualified call
    // may call function in the namespaces of T
    swap(a, b);
}

int main() {
    int a = 0;
    int b = 0;
    adl_swap(a, b); // calls std::swap

    A::S s1, s2;
    adl_swap(s1, s2); // calls A::swap
}

As you can see, the using namespace is inside the function, so it's not used globally, but only inside the function scope.

Also in the function adl_swap, we use function inside the A namespace. We don't actually name because it is implicit.

Another way to bring namespace symbols is to use symbols individually instead of the whole namespace:

using std::cout;

int main() {
    using std::endl;
    cout << "Unqualified names!" << endl;
}
like image 45
Guillaume Racicot Avatar answered Aug 14 '26 20:08

Guillaume Racicot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!