Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Namespace

Using directive

PreviousNamespace ExampleNextHeader File

Last updated 9 months ago

The using directive provides flexibility in how you work with namespaces, allowing you to balance between code readability and avoiding name conflicts. It's important to use these features judiciously, especially in larger codebases, to maintain clear and maintainable code.

  1. using namespace directive:

This brings all names from a namespace into the current scope.

#include <iostream>

namespace Math {
    int add(int a, int b) { return a + b; }
    int subtract(int a, int b) { return a - b; }
}

int main() {
    using namespace Math;
    
    // Now we can use Math functions without the Math:: prefix
    std::cout << add(5, 3) << std::endl;      // Outputs: 8
    std::cout << subtract(10, 4) << std::endl; // Outputs: 6
    
    return 0;
}

Run it .

  1. using declaration:

This brings a specific name from a namespace into the current scope.

#include <iostream>

namespace Math {
    int add(int a, int b) { return a + b; }
    int subtract(int a, int b) { return a - b; }
}

int main() {
    using Math::add;  // Only 'add' is brought into scope
    
    std::cout << add(5, 3) << std::endl;      // Outputs: 8
    // std::cout << subtract(10, 4) << std::endl; // This would cause an error
    std::cout << Math::subtract(10, 4) << std::endl; // This works: 6
    
    return 0;
}
  1. Namespace alias:

This creates an alternative name for a namespace, which can be useful for long namespace names.

#include <iostream>

namespace VeryLongNamespaceName {
    void someFunction() {
        std::cout << "Function in a long namespace name" << std::endl;
    }
}

int main() {
    namespace Short = VeryLongNamespaceName;
    
    Short::someFunction();  // Outputs: Function in a long namespace name
    
    return 0;
}
  1. Inline namespace:

This is a feature where names in the inline namespace are treated as if they were part of the enclosing namespace.

#include <iostream>

namespace Outer {
    inline namespace V1 {
        void foo() { std::cout << "V1::foo" << std::endl; }
    }
    
    namespace V2 {
        void foo() { std::cout << "V2::foo" << std::endl; }
    }
}

int main() {
    Outer::foo();     // Calls V1::foo
    Outer::V1::foo(); // Also calls V1::foo
    Outer::V2::foo(); // Calls V2::foo
    
    return 0;
}

Important considerations:

  1. Using using namespace in the global scope of a header file is generally considered bad practice as it can lead to name conflicts.

  2. using declarations are often preferred over using namespace directives as they're more specific and less likely to cause naming conflicts.

  3. Namespace aliases can improve readability when working with long namespace names.

  4. Inline namespaces are often used for versioning in library design.

The using keyword, when used judiciously, can make code more readable and manageable when working with namespaces. However, it's important to use it carefully to avoid naming conflicts, especially in larger codebases.

Run it .

Run it .

Run it .

🗺️
here
here
here
here