passing string to function

This is a discussion on passing string to function within the C++ Programming forums, part of the General Programming Boards category; In passing a string to a function that will not modify the original string, is there any real difference between ...

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

    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
    21,397
    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").
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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
    21,397
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 01:53 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21