Why boost :: call_traits <double> :: param_type is "const double &" and not "double"

I use the code "boost :: call_traits :: param_type" on the win32 program @windows 7 machine. To my surprise, this is not "double", but "const double &".

I thought that all primitive types are good to use "pass by value" for function parameters, isn't that common sense? Since many people will use is_pod to determine if a link is used or not, right?

+6
source share
2 answers

This optimization seems to apply only to the "small" built-in types, according to the doc .

With a quick look at the source, it looks like the "small" types are considered as satisfying sizeof(T) <= sizeof(void *) . On a 32-bit machine, this obviously does not include double .

In some cases, it might be better to pass parameters as double , rather than double const& , but, as always, you will need to test and test ...

+4
source

boost::call_traits class template was not specialized for double . The template of the primary class returns const T & , therefore it is const double & , not double , although in this case it does not matter much, but it matters if T is the type of the class that has huge data. To avoid an unnecessary copy, this is a const reference.

-1
source

Source: https://habr.com/ru/post/902613/


All Articles