Based on what I have compiled from the authors of the compiler, value types are much preferable to references / pointers in terms of efficiency.
This is due to the fact that value types are easier to talk about when you do not need to care about an alias, externally modified memory (to which the pointer belongs), the cost of dereferencing a pointer, etc. I must say that although I understand such problems, I still have a few questions regarding specific cases.
Case No. 0
void foo(const float& f) Well, we have a link here, but it is permanent! Of course, we have a constant representation (ref), so outwardly it can be a change, but it can only happen in a multi-threaded world, and I'm not sure that the compiler should take this into account at all if synchronization primitives are not used. Obviously, if inside we used another pointer / reference to any float variable, we could risk changing the f parameter. Can the compiler consider this parameter safe (unless we use ref / ptr to float inside)?
Case No. 1
void foo(vector<int> f) Speaking from the point of view of a C ++ 11/14 programmer, I know that vector can be safely moved to a function. As we all know, inside the container contains a pointer to an array. Will the compiler consider the pointer as safe (without external modifications), since we just received a copy of the vector, so we are its sole owners?
In other words: it is a copied object that is considered safe (since we logically create a clone of the object), or the compiler is not allowed to make such assumptions and any ptr / ref member should be considered as potentially dangerous, since the ctor / op copy may not have done the right thing a copy. Should a programmer not be responsible for processing shared resources when copying them?
Bottomline
Permanent pointers / links and copied complex objects are generally slower than copies of primitives, and therefore should be avoided as much as possible in critical critical code; or are they a little less effective and we don’t have to worry about that?
source share