Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cost of namespaces

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.

like image 959
Anthony Monterrosa Avatar asked Dec 05 '22 10:12

Anthony Monterrosa


1 Answers

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.

like image 179
eerorika Avatar answered Dec 29 '22 00:12

eerorika