Hacker News new | past | comments | ask | show | jobs | submit login

Where is the std:: part coming from / why is it not just print(...)?



C++ has namespacing which makes sense because this language has an enormous amount of available 3rd party libraries and without name spacing you can't help stepping on each others toes.

There are two ways you might want to have this work anyway despite namespacing. One option would be that you just import the namespace and get all the stuff in it, this is popular in Java for example, however in C++ this is a bit fraught because while you can import a namespace, you actually get everything from that namespace and all containing namespaces.

Because the C++ standard library defines a huge variety of symbols, if you do this you get almost all of those symbols. Most words you might think of are in fact standard library symbols in C++, std::end, std::move, std::array, and so on. So if you imported all these symbols into your namespace it's easy to accidentally trip yourself, thus it's usual not to do so at all.

Another option would be to have some magic that introduces certain commonly used features, Rust's preludes do this, both generally (having namespaces for the explicit purpose of exporting names you'd often want together) and specifically ("the" standard library prelude for your Edition is automatically injected into all your Rust software by default). C++ could in principle do something like this but it does not.


D avoids the stepping on each other problem by using modules.


Can D compile modules in parallel?


The language is designed so that is possible, although the current compiler does not. At one point, the compiler did all the file reading in parallel, but that was finally turned off because it did not significantly improve compile speed.


Hundreds of not thousands of people made their own print over the years. Namespacing is important.


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++)


> 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.


Oh, 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)


The function is part of the std namespace, just like std::cout.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: