Namespaces

Every time you type “std::” you’re using a namespace. Namespaces are useful because, similar to classes, they can allow you to place related code all in one place. They work just like folders on your computer because you use folders to store related files all together.

Here’s an example of a program that uses a “robot” namespace:

#include <iostream>
#include <string>

namespace robot {
    class Motor {
        public:
            void start(double speed) {
                std::cout << "Starting motor with speed: " << speed << "\n";
            }
    };

    std::string name = "My Robot";
}

int main() {
    robot::Motor frontLeftMotor;
    robot::Motor frontRightMotor;

    frontLeftMotor.start(0.8);
    frontRightMotor.start(0.5);

    std::cout << "Robot's name: " << robot::name << "\n";

    return 0;
}

Program output:

Starting motor with speed: 0.8
Starting motor with speed: 0.5
Robot's name: My Robot

We can access classes, variables, functions, etc. inside a namespace by first typing the namespace name and following that with two colons. Everything inside a namespace can be considered “public,” meaning that all code in a namespace is accessible outside of that namespace.

Use Cases

Namespaces are most useful when you have lots of related classes that you want to organize together. For example, you could use a “math” namespace if you have lots of classes dealing with mathematical operations.

You’ll also find namespaces used very often in code “libraries,” which are sets of code that you can download to make certain tasks easier. When programming our robot, we use a library called WPILib that has has classes already created for different sensors, controllers, and lots more. Most of these classes are placed inside the “frc” namespace for better organization.

Using Declarations

If you ever get tired of always typing namespace names like “std,” you can use a “using declaration” to automatically have access to code in that namespace.

#include <iostream>

using namespace std;

int main() {
    cout << "Hello world!\n";
    return 0;
}

Program output:

Hello world!

By “using” the std namespace, we don’t need to type “std::” before cout. Always be cautious when doing this, however. If you have pieces of code (variables, classes, functions, etc.) with the same name as something inside a namespace, then “using” that namespace would cause conflicts. This is because having multiple pieces of code with the same name in a single program would lead to ambiguity.

Because of this, it is often suggested that you only apply using declarations to specific pieces of code, rather than an entire namespace. Here’s what that looks like:

#include <iostream>

using std::cout;

int main() {
    cout << "Hello world!\n";
    return 0;
}

In this program, we only “use” the std namespace for cout. This means that we’d still need to type “std::” for everything else in the std namespace.

Example

When programming our robot, we use a class called “WPI_VictorSPX” for interacting with motors. However, it’s located inside one namespace that’s located inside another namespace that’s located inside another namespace (and this goes on…). To use the class, we need to type “ctre::phoenix::motorcontrol::can::WPI_VictorSPX.” That’s a lot of namespaces!

To clean this up, we write “using ctre::phoenix::motorcontrol::can::WPI_VictorSPX;” at the top of our code. That way, we can refer to the class just as WPI_VictorSPX.