[cfe-dev] Inlined function that pass POD by reference

Francois Fayard via cfe-dev cfe-dev at lists.llvm.org
Wed Jan 11 01:49:51 PST 2017


Hi,

Because of the emphasis on generic programming, the C++ standard library has many weird signatures such as:

const int& std::min(const int& a, const int& b) (1)

where one would expect a:

int std::min(int a, int b) (2)

I thought that this would not matter from a performance point of view as those functions are inlined anyway. And it turns out that when you replace a signature such as (2) into (1) where the function is inlined, the assembly does not change at the call site: both are inlined properly.

But I came to realize that some side effects are cascaded to other parts of the code, and codes that use (1) end up being slower than code using (2), wether the compiler is clang, gcc or intel compiler (even though the loss of performance differs from compiler to compiler).

Is there any fundamental reason for this? Would you recommend avoiding signatures such as (1) which end up being used a lot when min is template-defined?

Best regards,
Francois


More information about the cfe-dev mailing list