On Monday, 11 January 2021 at 15:23:23 UTC, Ola Fosheim Grøstad wrote:
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.

I meant allocation... The following prints "1", so no allocation.

#include <iostream>
#include <string>

std::string a("hello world");

void f(std::string b){
    std::cout << (b.data() == a.data()) << std::endl;
}

int main()
{
    f(a);
}


Reply via email to