Given the code
// somewhere in the program
const char* p1 = "Hello World";
// somewhere else in the program
const char* p2 = "Hello World";
is there a way to ensure that p1 == p2
is always satisfied within the entire program / library? By that I mean that p1
and p2
always refer to the same string literal.
The Reason behind it
What I'm trying to achieve is to use const char*
as a key for std::map<const char*, something>
. I have a macro
#define nameof(id) #id
that mimics the behavior of the nameof
keyword in C# (I know this is already flawed) and I want to use it to access a registry like structure, for example
void foo()
{
auto x = getMapping(nameof(foo));
}
// different place in code
void registerFoo(something x)
{
setMapping("foo", x);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…