C++ Function Parameter

Started by
10 comments, last by Matt-D 11 years, 6 months ago
Np, sorry for the initial confusion and spread disinformation. laugh.png
[size=2]I edited the original post, not to change what I said, but to visually strike-out the wrong info and add a link to this thread.

The Lavavej video at ~35:00 indicates the proper time to use pass-by-value would be when you are creating a copy from the const ref anyway, because then with temporaries the copy can be avoided; as STL puts it in other places: "Never slower, but sometimes faster".

So, general rule of thumb seems to be:

  • Default to const ref, unless...

    • ...you need to modify the original, then default to regular ref
    • ...you need to make a copy, then default to by-value which will sometimes (depending on move constructor) optimize away the copy for temporaries.


Which was pretty much the same rule of thumb C++ has always had. smile.png
Advertisement
Read this (including the references -- Boris' three-parter "Efficient argument passing in C++11" is linked to in the blog post):
http://scottmeyers.blogspot.com/2012/10/parameter-types-in-constructors.html

This topic is closed to new replies.

Advertisement