If I have a template function like
template<typename T>
void doSomething(const T arg) {.....}
then I do not know ahead of time if T is a simple int or if it is a huge struct. If it is an int, passing by value makes sense, while the struct should be passed by reference.
How do I design a template function which works sensibly with both types of argument?
The boost::call_traits
library has several helpers for stuff like this. In particular, call_traits<T>::param_type
Defines a type that represents the "best" way to pass a parameter of type T to a function.
You use it like so:
template<typename T>
void doSomething(typename boost::call_traits<T>::param_type arg) {.....}
It basically works by specializing the class for speacial cases. So, e.g., your int
case from the question is (indirectly) specialized to int
(and not int &
).