== comparison of string literals, and their usage
diniz
diniz at posteo.net
Sat Apr 6 15:35:22 UTC 2019
Le 06/04/2019 à 16:07, AltFunction1 via Digitalmars-d-learn a écrit :
> On Friday, 5 April 2019 at 14:49:50 UTC, diniz wrote:
>> Hello,
>>
>> Since literal strings are interned (and immutable), can I count on the fact
>> that they are compared (==) by pointer?
>
> No. "==" performs a full array comparison and "is" is apparently simplified at
> compile time. In the compiler there's no notion of string literal as a special
> expression. It's always a StringExp. See https://d.godbolt.org/z/K5R6u6.
>
> However you're right to say that literal are not duplicated.
Thank you very much.
So, I still could store and use and compare string pointers myself [1], and get
valid results, meaning: pointer equality implies (literal) string equality. Or
am I wrong? The point is, the parser, operating on an array of prescanned
lexemes, will constantly check whether a valid lexeme is present simply by
checking the lexeme "class". I don't want that to be a real string comp, too
expesensive and for no gain.
[1] As in the second comp of your example:
void main()
{
auto c2 = "one" == "two";
auto c1 = "one".ptr is "two".ptr;
}
--
diniz {la vita e estranj}
More information about the Digitalmars-d-learn
mailing list