Browse files

fix cppcheck "Function parameter 'i' should be passed by reference."

  • Loading branch information...
1 parent 7754fb7 commit bfc73ad385b0da3f80ace71966f4e2802f2d6800 @abma abma committed Aug 10, 2012
Showing with 4 additions and 4 deletions.
  1. +4 −4 rts/System/SafeVector.h
View
8 rts/System/SafeVector.h
@@ -22,23 +22,23 @@ class safe_vector : public std::vector<T>
safe_vector(size_type size, T value): std::vector<T>(size, value), showError(true) {}
safe_vector(const safe_vector<T>& vec): std::vector<T>(vec), showError(true) {}
- const T& operator[] (const size_type i) const {
+ const T& operator[] (const size_type& i) const {
if (i >= std::vector<T>::size())
return safe_element(i);
return std::vector<T>::operator[](i);
}
- T& operator[] (const size_type i) {
+ T& operator[] (const size_type& i) {
if (i >= std::vector<T>::size())
return safe_element(i);
return std::vector<T>::operator[](i);
}
- const T& at (const size_type i) const {
+ const T& at (const size_type& i) const {
if (i >= std::vector<T>::size())
return safe_element(i);
return std::vector<T>::at(i);
}
- T& at (const size_type i) {
+ T& at (const size_type& i) {
if (i >= std::vector<T>::size())
return safe_element(i);
return std::vector<T>::at(i);

7 comments on commit bfc73ad

@abma
Spring RTS member

awww. damnit, that was wrong? i will be always an basic type? (int/short/...)?!(if yes ->i'll revert!)

@zerver

does not matter much, but yes "i" is always of basic type

@jk3064
Spring RTS member

it matters, in case size(type) == size(void*) references are slower than copies.
Cause in case of references it puts back the pointer address on the stack, meaning when you access the value it needs to dereference the pointer first.
Instead it could just push the basetype itself on the stack and cause it isn't bigger than the pointer you won't lose memory nor will it cause a ctor calling.

And here it is used for the index-operator which are called very often, so it matters.

@zerver

Yeah, in debug mode or if the compiler does not optimize at all

@jk3064
Spring RTS member

it cannot optimize function arguments esp. when they are defined in other .obj

@zerver

I know, but in this case it is inline, so the compiler can do all kinds of hacks.

Please sign in to comment.