[Style] Converting from char[] to string, to!string vs idup

Steven Schveighoffer schveiguy at yahoo.com
Tue Mar 25 16:19:22 PDT 2014


On Tue, 25 Mar 2014 19:13:07 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> On Tue, Mar 25, 2014 at 10:02:33PM +0000, monarch_dodra wrote:
>> On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:
>> >Is there a performance benefit? Is it simply because it's more general?
> [...]
>> There is *1* thing you should take into account though: "to!" is a
>> no-op for string=>string or char[]=>char[], or anything else that can
>> be implicitly converted as such. In contrast, "dup"/"idup" will create
>> an actual copy.
>>
>> Not that this is good or bad. Just something you should keep in mind.
>
> I think it's a good thing. It avoids needless copying where it's not
> necessary. (Of course, if you have a char[] and you actually want a new
> copy, then you have to use .dup explicitly.)

In the case of char[] -> char[], you explicitly want to dup. The original  
text is part of a buffer that is reused.

-Steve


More information about the Digitalmars-d-learn mailing list