4 ms·
I don't get why C++ programmers like to keep the namespace prefixes around. I'd just add lots of "using ..." statements and make my code more readable. What's t
by haolez 7y ago
I don't get why C++ programmers like to keep the namespace prefixes around. I'd just add lots of "using ..." statements and make my code more readable. What's the chance of conflicts anyway when you are already dealing with a tiny fraction of the code in the current file?
- rlp 7y agoIt's a header-only library, it would effectively add "using" statements anything that included the library.
- haolez 7y agoOuch. Seems like an unsolvable limitation at this point. Nice catch.
- colejohnson66 7y agoC++20 introduces “modules” which are an attempt at alleviating the problem of headers. Support, though, is lacking in major compilers’ master branch. GCC supports it if you build a certain branch from source though.
- LessDmesg 7y agoSome really prompt bugfixing on C++ committee's part. "We have a broken compilation process that leads to humongous build times and namespace pollution! Oh, OK, let's wait another 20 years to fix it". I guess C++ coders deserve the std::'s their language gives them.
- Reelin 7y agoIt's actually possible to avoid this! namespace foo::_bar { using namespace ::acme::widgets; /* internal helpers go here */ inline namespace exports { /* public API goes here */ } } namespace foo::bar { using namespace _bar::exports; }
- mikepurvis 7y agoBesides the header issue mentioned in the sibling, sometimes the context is important too, for an example from Python, no one would `from os.path import join`— you always use `os.path.join` in its entirety.
- deleted 7y ago[deleted]
- tomjakubowski 7y agoI agree with your point and I'm just nit picking, but `from os import path` and `path.join` is pretty acceptable too.
- david2ndaccount 7y agoI have definitely seen `from os.path import join` in some ugly ML github repos.
- pfranz 7y ago:shrug: I've done that quite a bit. This randomly selected popular Python library does it, too[1]. The example for `os.walk` literally has `from os.path import join, getsize`[2]. The docs seem to back this up[3]: "Remember, there is nothing wrong with using `from package import specific_submodule!` In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages." I haven't dealt with a lot of C++ code but I feel like I see a lot of `using namespace ...` and haven't seen much `using ...;` (which I think is more readable/acceptable) [1] https://github.com/psf/requests/blob/df918c066fa275abc2bb0c960647dc064d136a01/requests/utils.py#L22 https://github.com/psf/requests/blob/df918c066fa275abc2bb0c9... [2] https://docs.python.org/3/library/os.html#os.walk https://docs.python.org/3/library/os.html#os.walk [3] https://docs.python.org/3/tutorial/modules.html#importing-from-a-package https://docs.python.org/3/tutorial/modules.html#importing-fr...
- eska 7y agoYou could do that in .cpp files, but it's objectively bad to do so in the header files due to namespace pollution. And now you have an inconsistency between headers and .cpp files..
- LessDmesg 7y agoWhy do header files even exist and why do sane languages work without them? Oh, I forgot, you cannot question The Holy Warts of Stroustrup. It's just C++. Leave all hopes of developer productivity at the door.