What is the cost of defining namespaces in c++? By that I specifically mean compile-time/run-time increase, and/or memory footprint. Let's say I have trivial program with a header file and a .cpp with main. Would I notice a change in these aforementioned statistics if I, say, define a namespace
in my header? What about 10, 100, or 1,000 namespaces? What if they are defined but never used: does that change the answer? I guess you could say I'm curious how this construct interacts with the compiler(s).
Similarly, I am curious about the effect of struct
/class
definitions and using
, but I couldn't think of a good title for a question encompassing all three.
What is the cost of defining namespaces in c++?
You have to write longer names†. This cost is offset by the fact that name conflicts are avoided across different namespaces. A short name is of no use if the name has a conflict.
†Not really though: A namespace lets you get off with less writing, since you don't always need to retype the namespace into unqualified identifiers.
By that I specifically mean compile-time
Potentially marginally.
run-time increase
None in practice.
Would I notice a change in these aforementioned statistics if I, say, define a namespace in my header?
You can find out by measuring.
You probably won't notice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With