As I know compiletime C-like strings are kept in static memory as only one instance. For instance I got both true
on gcc 4.6 running example below. But I wonder is
You're comparing two different string literals, which happen to have the same value. According to the C++ standard, it is implementation defined whether identical string literals occupy the same memory or not (which means that the implementation must document what it does); according to the C standard, it is unspecified. (I presume that the C++ standard would allow the implementation to document something along the lines of "string literals of identical content share the same instance if they are in the same translation unit, and do not share the same instance otherwise.)
If your goal is to be able to just compare pointers, the usual solution is to use a function (static if it is a class member) which returns the string literal:
char const*
value()
{
return "Hello";
}
bool
isHello( char const* str )
{
return str == valule;
}
and then ensure that all instances of the string are obtained by
calling value()
.