Monday, September 9, 2013

Automatically choose between pass by value and pass by reference

Automatically choose between pass by value and pass by reference

I have a class with a template parameter T. Some function of the class get
a parameter with type T. To achieve a maximum speed, I want to pass it by
value if T is a built in type and pass it by const reference in other
cases. I know that this is achievable with some template meta programming,
but I am looking for some kind of feature in the standard C++ library
(C++11 is also welcomed) what support it (in compile time), or make it
easier to implement.
I want to write a code like this, where get_type is T if T is a built in
type and get_type is const T& if T is anything else:
template <typename T> void foo(get_type<T> a);
P.S.: I know Loki (http://loki-lib.sourceforge.net/) have a function for
it, but I don't want to make my code dependent on an external library.

No comments:

Post a Comment