3 ms·
The std namespace is from the <print> standard header. It’s not just print because while you might want it in the global namespace, other people do not. For exa
by dkersten 3y ago
The std namespace is from the <print> standard header. It’s not just print because while you might want it in the global namespace, other people do not. For example, my code isn’t cli and doesn’t need to print to the cli, but perhaps I want to print to a printer or something else and have my own print function.
Leave un-namespaced identifiers to those that are declared in the current file and namespace everything else. If you really want, you’re free to add “using namespace std” or otherwise alias the namespace, but keeping standard library functions out of the global namespace as a default is a good thing! (In any language, not just C++)
- JohnFen 3y ago> If you really want, you’re free to add “using namespace std” You're free to, but I discourage the habit. It's more verbose to add the namespace:: prefix to symbols, but it sure does make it easier on the devs that have to work with the code later.
- dkersten 3y agoOh, I’m completely with you on this and always prefix my namespaces. Occasionally I will alias long namespaces to short ones, but I never pull identifiers into the global scope and I really dislike when I see code online that does “using namespace” (unless it’s tightly scoped, at least). I’ve been prefixing std:: for years and won’t stop now, I like knowing where an identifier is coming from, which is extra important when you have multiple types of similar containers that you use for different performance characteristics (eg abseil, folly, immer versions of containers vs std containers)