Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I pass a std::string to a function?

Tags:

c++

Should I always pass a std::string by const reference to a function if all that is done inside that function is to copy that string? Additionally, what is the difference (perf or otherwise) between passing by value and passing by reference? As I understand, one uses operator= and the other copy constructor. Is that the case?

like image 473
nakiya Avatar asked Oct 11 '10 05:10

nakiya


People also ask

How do you pass std::string to function in C++?

I think it depends on what he means by making a copy. If it's to, say, a member then it should be passed as a reference and just copied to the member. In C++0x, do it by value and std::move it into the member.

Should you pass std::string by reference?

I believe the normal answer is that it should be passed by value if you need to make a copy of it in your function. Pass it by const reference otherwise.

How we pass a string into a function?

To pass a one dimensional string to a function as an argument we just write the name of the string array variable. In the following example we have a string array variable message and it is passed to the displayString function.

Can we pass string in function?

Passing Strings to FunctionsStrings can be passed to a function in a similar way as arrays. Learn more about passing arrays to a function.


2 Answers

Don't believe everything you read on the internet. It is better to pass by const reference. To give proof, I wrote a test program...

test.cpp:

#include <ctime>
#include <iostream>
#include <string>

void foo(std::string s);
void bar(const std::string& s);

int main() {
    const std::string s("test string");

    clock_t start = clock();
    for (int it = 0; it < 1000000; ++it)
        foo(s);
    std::cout << "foo took " << (clock() - start) << " cycles" << std::endl;

    start = clock();
    for (int it = 0; it < 1000000; ++it)
        bar(s);
    std::cout << "bar took " << (clock() - start) << " cycles" << std::endl;
}

aux.cpp:

#include <string>
std::string mystring;

void foo(std::string s) { mystring = s; }
void bar(const std::string& s) { mystring = s; }

Compiled with 'g++ -O3 test.cpp aux.cpp' and got the printout:

foo took 93044 cycles
bar took 10245 cycles

Passing by reference is faster by an order of magnitude.

like image 186
Johan Kotlinski Avatar answered Oct 01 '22 18:10

Johan Kotlinski


Should I always pass a std::string by const reference to a function if all that is done inside that function is to copy that string?

No. If you are going to just copy the string inside of the function, you should pass by value. This allows the compiler to perform several optimizations. For more, read Dave Abraham's "Want Speed? Pass by Value."

What is the difference (perf or otherwise) between passing by value and passing by reference? As I understand, one uses operator= and the other copy constructor. Is that the case?

No, that is not at all the case. A reference is not an object; it is a reference to an object. When you pass by value, a copy of the object being passed is made. When you pass by reference, a reference to the existing object is made and there is no copy. A good introductory C++ book will explain these basic concepts in detail. It is critical to understand the basics if you want to develop software in C++.

like image 25
James McNellis Avatar answered Oct 01 '22 17:10

James McNellis