properly passing strings to functions? (C++ vs D)

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Jan 11 15:23:23 UTC 2021


On Monday, 11 January 2021 at 14:12:57 UTC, zack wrote:
> A beginner question: How to pass strings properly to functions 
> in D?
> Is there any allocation going on if just use a function as 
> "myPrint"? In C++ I have often seen calls where one just passes 
> a reference/const reference to a string to avoid allocation.

C++ strings are reference counted, I think, so it is more to 
avoid a reference count increment than to avoid a collection.


> A D-Style String could be seen as "const(char)[]"? So as it is 
> a slice it already is a kind of reference to some data 
> elsewhere? Which means calling a function like "myPrint" in D 
> wouldn't cause any allocation. Is this correct?

D strings are backed by GC so they are passed by reference too.




More information about the Digitalmars-d-learn mailing list