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

●5 ●16 ●83
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";
    // ...
}
Part 27 of 28 in Tuesday Coding Tips
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

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

Tuesday Coding Tip 08 — Explicit template specialization

Jakub Neruda - Apr 21
chevron_left
1.9k Points • 104 Badges
Brno, Czech Republic • linkedin.com/in/jakub-neruda
35Posts
35Comments
21Connections
Experienced C++ developer, team lead and hobby gamedev. I enjoy writing stuff about C++, clean code, clean architecture, and game development.

Related Jobs

View all jobs →

Commenters (This Week)

3 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!