Quote:Original post by CTar
I have created a structure with can give you type "const T&" if sizeof(T)>sizeof(T&) and type "const T" if sizeof(T)<=sizeof(T&).
There are a couple of problems with this. For one, sizeof( T& ) is required to yield the size of T,
not the sizeof a reference, so sizeof(T) will always equal sizeof(T&) in C++.
Secondly, it's not just the size of the type which should affect your decision, it's more whether or not the type also has nontrivial copy construction and destruction. For instance, a smart pointer may be the size of a regular pointer, however copying and destroying smart pointers can be costly, since it can imply things like incrementing and decrementing reference counts, etc. Because of this, it can be more efficient to pass by reference. As well, what about types which can't be copied? In such a case, a reference has to be be used regardless of its size and destructor, etc.
Anyway, the simplest rule to follow is that if you are unsure which to use, use a reference to const, since reference arguments are extremely easily optimized away with inlining.
Alternatively, if you are really interested in attempting to determine which method to use, you should check out
Boost.CallTraits, which accomplishes the type of logic similar to what you described.