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

Francois Fayard via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 12 01:05:03 PST 2017


Thanks,

I am quite sure I had this problem but now, I just can’t seem to reproduce it as I don’t have the exact code anymore. The problem appeared in a much more complicated code and I can’t make it show this difference again. I’ll try hard again and come back when I have a code that show this difference.

François Fayard

> On Jan 11, 2017, at 12:41 PM, mats petersson <mats at planetcatfish.com> wrote:
> 
> It would be good to have an example of code where this is actually affecting things. In general, clang (or rather LLVM) is good at "unreferencing" references that don't need to be references when inlining, but of course, there are probably some cases where this does not hold true.
> 
> [I expect that the reason for things like `const int& std::min(const int &a, const int& b)` is because the function is actualy declared as "template<typename T> const T& std::min(const T& a, const T& b)`, rather than as an `int` function directly.
> 
> I certainly would expect something like:
> 
> int main()
> {
>     int a, b;
>     std::cin >> a >> b;
>     std::cout << std::min(a, b) << std::endl;
> }
> 
> to produce identical code whether std::min(int a, int b) or the const reference variant. But of course, with more complex constructs, the compiler may not realize what is going on and be able to remove the references. Making the compiler  smarter in those cases is always an option... 




More information about the cfe-dev mailing list