"Unusing" Identifiers

For background, consider the standard namespace. In competitive programming, frequent practice is

using namespace std; 

This is practical: we no longer need to prefix every identifier with std::. Terrible for production of course, but invaluable when time is of essence.

Of course, the reason this is considered bad practice is if you want to use something the std already predefines (especially an issue if you use bits). For instance

#include <bits/stdc++.h>
using namespace std; 

This code makes y1 unusable because it is already a symbol for a mathematical distribution in cmath. Naturally, one would want something like

#include <bits/stdc++.h>
using namespace std; 
not using std::y1; 

Unfortunately, however, this doesn't exist.

But there is a clever solution. In this case, we can literally use a find-and-replace solution to escape the y1 problem:

#include <bits/stdc++.h>
using namespace std; 
#define y1 _y1

Now, every y1 is replaced with _y1, which the compiler sees as nothing more than a variable name. This means that you can safely use y1 (while preprocessor will use the other version, a semantically safe alternative).

Alas, this extends and can be applied to anything one wants to "unuse" from the standard. For example, this gives you to opportunity to use "rank" instead of "rnk" in your functions.

Last updated