If all functions must have a value with a parameter, see its value, shouldn't you always pass this parameter with a constant reference?
One of my colleagues stated that this does not matter for small types, but I do not agree.
So, is there any advantage to this:
void function(char const& ch){
if (ch == 'a'){
DoSomething(ch);
}
return;
}
above this:
void function(char ch){
if (ch == 'a'){
DoSomething(ch);
}
return;
}
They seem the same to me:
#include <iostream>
#include <cstdlib>
int main(){
char ch;
char& chref = ch;
std::cout << sizeof(ch) << std::endl;
std::cout << sizeof(chref) << std::endl;
return EXIT_SUCCESS;
}
But I do not know if this is always the case. I believe that I am right, because he does not create any additional overhead costs and documents it himself. However, I want to ask the community if my reasoning and assumptions are correct?
source
share