Tuesday Coding Tip 27 — Aliasing and ‘using’ in C++

●5 ●27 ●93
calendar_today • schedule1 min read
— Originally published at medium.com

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


Every hello world tutorial starts with the command using namespace std;. This command is dangerous as it allows you to bring all standard symbols into your namespace where they can collide with your own symbols. Depending on the kind of collision, the compiler might not tell you, nor can you be sure which version will be used.

#include <stdexcept>

int min(int a, int b)
{
    std::terminate();
}

#include <cmath>

int main() {
    using namespace std; // dangerous
    // better to import specific names like:
    using std::max;

    return min(10, 20); // which min will be called?
    // ::min would disambiguate to the local function
    // std::min would disambiguate to standard
}

It is better to use the using keyword to selectively bring symbols into the scope or even alias them to be sure they’re unique (note that namespaces alias differently).

#include <filesystem>

// Aliasing namespace works differently
namespace fs = std::filesystem;

// from aliasing classes
using Path = fs::path;

bool fileExists(const Path& path)
{
    return fs::exists(path);
}

The keyword can also be used to expose class template parameters for common usage.

template<class T>
class Foo {
public:
    // Exposing template type saves time
    // to those who need to get to it
    using DataType = T;

    T getItem() const { return T{}; }
};

int main() {
    Foo<int> foo;
    decltype(foo)::DataType item;
    item = foo.getItem();
}

And since C++20, you can bring enum values (selectively or in group) for better ergonomics in enum-based branching.

enum class EmployeeType {
    JuniorDev,
    MediorDev,
    SeniorDev,
    ScrumMaster,
    ProductManager
};

std::string to_string(EmployeeType et) {
    switch (et)
    {
        using enum EmployeeType; // Since C++20
        case JuniorDev: return "junior";
        case MediorDev: return "medior";
        // ...
    }
}

std::string to_string(EmployeeType et) {
    using EmployeeType::JuniorDev;

    if (et == JuniorDev) return "junior";
    // ...
}

Want more #programming & #programming deep dives?

Join 5,973+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 27 of 31 in Tuesday Coding Tips

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 17 — Wrapping C APIs

Jakub Neruda - Jun 30

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31

Let's play with C++ reflection

Jakub Neruda - Nov 12
chevron_left
2.1k Points • 125 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
42Posts
36Comments
22Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Related Jobs

Commenters (This Week)

2 comments
2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!