7

Do they work across different object files? Do they work across different DLLs?

I know this depends on the compiler. I'm curious if there are any compilers and optimization settings that will make this work.

Sarien
  • 6,647
  • 6
  • 35
  • 55

1 Answers1

3

Normally, yes, but in principle, using Link-Time-Optimization (-flto for GCC/Clang compilers and linkers) or Link-Time-Code-Generation (/LTCG and /GL for MSVC's compiler and linker), the compiler and linker can leverage their shared knowledge and perhaps inline code and elide copies. GCC's manual states:

[...] this causes all the interprocedural analyses and optimizations in GCC to work across the two files as if they were a single one. This means, for example, that the inliner is able to inline functions in bar.o into functions in foo.o and vice-versa.

Note this will not work with DLLs, because a shared library's code is fixed and already fully compiled.

RVO only needs information about the function itself (as it constructs the function's return value in-place instead of copying/moving on return. This will likely work without the aboce options.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • but not between modules – tenfour May 21 '14 at 08:36
  • @tenfour: define "modules"; for static libraries and object files, you're wrong, for shared libraries (so/DLL), you're right. Added that to my answer. – rubenvb May 21 '14 at 08:37
  • in a windows context, "module" means a PE file... DLLs, OCX, EXE, etc. So we agree :D – tenfour May 21 '14 at 09:01
  • @tenfour Oddly, the link that Jerry posted above claims that it even works for DLLs (RVO at least) – Sarien May 21 '14 at 10:59
  • true, and maybe some compilers would make it happen. But true RVO requires cooperation between caller & callee, so I don't see how it's possible in any general sense. When it comes to exporting C++ features across DLL bounds, things get very precarious and implementation-specific – tenfour May 21 '14 at 11:27
  • Yes, passing values across boundaries can cause trouble anyway because the allocators can differ. – Sarien May 21 '14 at 13:00