Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • i found your rules good but im not to sure about the first part where u talk about not passing it as a ref would speed it up. yeah sure, but not passing something as a ref just cus of optimaztion doesnt make sense at all. if you want to change the stack object you are passing in, do so by ref. if you dont, pass it by value. if you dont wanna change it, pass it as const-ref. the optimization that comes with pass-by-value should not matter since you gain other things when passing as ref. i dont understand the "want speed?" sice if you where gonna perform those op you would pass by value anyway.. Commented May 2, 2012 at 8:14
  • Johannes: I loved that article when I read it, but I was disappointed when I tried it. This code failed on both GCC and MSVC. Did I miss something, or does it not work in practice? Commented Aug 14, 2012 at 21:50
  • I don't think I agree that if you want to make a copy anyway, you'd pass it by value (instead of const ref), then move it. Look at it this way, what's more efficient, a copy and a move (you can even have 2 copies if you pass it forward), or just a copy? Yes there are some special cases to either side, but if your data cannot be moved anyway (ex: a POD with tons of integers), no need for extra copies. Commented Oct 14, 2012 at 6:29
  • 2
    Mehrdad, not sure what you expected, but the code works as expected Commented Oct 14, 2012 at 6:35
  • I'd consider the necessity of copying just to convince the compiler that the types don't overlap a deficiency in the language. I'd rather use GCC's __restrict__ (which can also work on references) than do excessive copies. Too bad standard C++ hasn't adopted C99's restrict keyword. Commented Jul 9, 2017 at 7:31