String literals have only one instance?

Jonathan Davis jmdavisprog at gmail.com
Thu Aug 19 02:44:46 PDT 2010


On 8/19/10, Rory Mcguire <rjmcguire at gm_no_ail.com> wrote:
> Are all string literals that have the same value initialized to the same
> address?
>
> void main() {
> 	string same() {
> 		return "This";
> 	}
> 	assert("This" is same());
> 	assert("This" is "This");
> }
>
>
> Can this be relied upon?
>

Well, since in Windows at least, string literals can be concatenated
to and whatnot, I very much doubt that there's any sharing involved.
You can always check with the is operator though. If it reports true,
then the two strings have the same instance. If it reports false, then
they don't.


More information about the Digitalmars-d-learn mailing list