Zebrafish
Zebrafish

Reputation: 13886

Does using C++ modules eliminate many compiler optimizations as opposed to inlining in header file?

Let's just say I have two options for how to define a function. I can define it in a header file and include that header file. Or I can define that function in a C++20 module, export it, and then import it in the translation unit that needs it. Since in the header it's defined inline the compiler can "see" and inline it no problem. Generally if it's not defined inline in the header file technically the compiler can still inline it as a part of link-time optimization, (I think the linker calls the compiler again to do this at link stage).

In the case of modules, are we basically talking the same thing as the latter I just mentioned, ie., a module is basically like a compiled object file and the compiler can't "see" it, until the link-stage, that is. What effect does this have on optimizations that the compiler does?

Upvotes: 2

Views: 152

Answers (1)

Farid Ullah
Farid Ullah

Reputation: 69

Yes with modules, functions are compiled separately, like object files, and the compiler only "sees" them during the link stage. Optimizations like inlining happen at link time, similar to link-time optimization (LTO) in headers. So, modules optimize later but work similarly to LTO

Upvotes: 1

Related Questions