reference/value in for loop #1884
Replies: 2 comments 2 replies
-
This is a difference between C++ and Carbon. You're right that you need to specify in C++; bear in mind that this doesn't work well in templates though, where one or the other needs to be chosen. Developers also aren't universally aware of the performance tradeoff of a given choice. The thought is that maybe we can make it so Carbon better at making the right choice without requiring the developer to fully understand and correctly specify their choice, also making generics and templates perform better in general. |
Beta Was this translation helpful? Give feedback.
-
The specifics of whether binding are copies, moves, or references is discussed in #821 (still pending) , #1830 , and https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/README.md#value-categories-and-value-phases (provisional) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Given the below sample code, it seems
e
is a reference to the element ins
, this will have performance penalty if e is a simple primitive such as integer. in C++, we can specify if we want to iterate with reference or value.for (e : T in s) {
if (e <= s.Last()) {
++i;
Swap(&s[i], &e)
}
}
Beta Was this translation helpful? Give feedback.
All reactions