Thread: passing string to function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    passing string to function

    In passing a string to a function that will not modify the original string, is there any real difference between
    Code:
    void myfunc( const string& source ) {
    ...
    }
    versus
    Code:
    void myfunc( string source ) {
    ...
    }
    I read somewhere that most compilers won't actually make a copy of the string if it's not being altered. If that's true, it seems that these two forms would be equivalent. Is that true?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Interestingly, someone from another message board community sent me an email recently with this message:
    Quote Originally Posted by lab1
    I saw you mentioned before that most compilers will not do extra copies when passing by value instead of const reference if the value is not changed. Do you know how intelligent the optimizer is? Like how deep in the code will it check this?
    I do not recall what exactly I stated, but what I do recall quoting from Meyers is that if you use pass by value when you intend to make a copy anyway, the compiler may be able to better optimise the code.

    I reason that if you do not intend to make a copy, then relying on compiler optimisation to elide the copy construction seems like unnecessary risk, and you are also not stating what you mean ("I know that it looks like I want to copy, but actually I don't want to copy").
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Good point. I suppose making it explicitly const is clearer. Thanks.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by R.Stiltskin View Post
    Good point. I suppose making it explicitly const is clearer. Thanks.
    Furthermore with the const in the argument declaration, you're also following const correctness paradigms. If you go without the const, you won't be able to pass a const string to your function without an unnecessary cast (your function doesn't change the argument after all).
    Best practice, afaik, is to make everything const that you don't intend to change, i.e. arguments, STL iterators, etc.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuantumPete
    If you go without the const, you won't be able to pass a const string to your function without an unnecessary cast (your function doesn't change the argument after all).
    Since pass by value is the alternative under consideration, that is not true, as a copy of the const object would be made.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    However, without making the parameter const, you could change it in the function body inadvertently. Of course, if that is your worry, then the discussion should be between (const string& source) and (const string source).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM