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

zack no-way-zack at no-web.de
Mon Jan 11 14:12:57 UTC 2021


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++:
void myPrintCPP(const std::string& input){ ... }

D:
void myPrint(string text){ ... }
void myPrintRef(ref string text) { ... }

So the question is does a function call like (ref string ...) 
(myPrintRef) make any sense in D to avoid additional allocations?

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?

Thank's for your help.



More information about the Digitalmars-d-learn mailing list