user25749542
user25749542

Reputation:

Why is the C++ standard library imported as a single module instead of multiple smaller modules?

If I wanted to only use std::string, for example, wouldn't importing a (theoretical) std::string module be more performant than importing the entire standard library?

Is it for ease of use, since you have to build the standard library module yourself and building each individual component you want into a module would be tedious?

Or is it due to some sort of overhead from creating a module (of any size), where if you import enough smaller modules, it would eventually be more expensive than just importing one large one?

Upvotes: 2

Views: 289

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 474076

If I wanted to only use std::string, for example, wouldn't importing a (theoretical) std::string module be more performant than importing the entire standard library?

That's not how modules work. Module importing is not like header inclusion. The cost of a module import is not proportional to how much stuff is in that module.

Importing a module means that the compiler reads some binary data saying what names are available. But the entire compiled code of the module need not be read just from the import. When you use an entity whose name is exported by a module, the compiler can then jump to that part of the compiled module file to find out what's in it.

Upvotes: 2

Alexandr Gritsenko
Alexandr Gritsenko

Reputation: 19

According to the new standard you just need to import std. But it doesn't means that whole STL will be added to your binary. Your compiler should optimize it, and "cut off" all useless links.

Upvotes: 1

Related Questions