30. Aliasing Namespace

#include <iostream>
using namespace std;

namespace Communication {
    namespace I2C {
        int speed = 400000; // 400 kHz
    }
}

int main() {
    namespace CI = Communication::I2C;
    cout << "I2C Speed: " << CI::speed;
    return 0;
}

 

Solution Explanation

  • namespace CI = Communication::I2C; creates an alias.
  • Instead of writing Communication::I2C::speed, we can simply write CI::speed.
     

Layman’s Terms

It’s like making a shortcut folder on your desktop so you don’t have to keep going through a long path every time.


Significance for Embedded Developers

Embedded projects often have deeply nested namespaces for hardware abstraction layers (HALs).

Aliasing makes code shorter and easier to read without losing clarity.

Loading...

Input

Expected Output

I2C Speed: 400000