A general approach would be to place all code that had the requirement of "do not replace TOKEN1" into a single solitary source file, and then not include the header file that defines the replacement into that file.
If the arbitrary sequence of characters were a single token (as it was before you edited your question), you can do this:
#define TOKEN2 TOKEN1
if (you_really_mean(TOKEN1)) {
//...
}
#undef TOKEN2
But, this solution is limited, since you run into problems if TOKEN2
had already been redefined to something else before.
If you have full control over what is being defined and what not, you can do this:
#define TOKEN1 TOKEN2
#define TOKEN2 <arbitrary sequence formerly assigned to TOKEN1>
Then, in your code to escape TOKEN1
:
#undef TOKEN1
//... code where you don't want TOKEN1 replaced
#define TOKEN1 TOKEN2
For your particular problem for handling overloaded new
, I find that largely, overloaded new
implementations are mostly identical (because they are usually just boiler plate code changing the allocator to use something other than system heap). If this is the case for you as well, you can put the overload definitions into an unguarded header file. This header file can #undef
the definition of new
and then redefine it again after the overload definitions.
Then any class can include this header file.