Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alex has given you <a href="https://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2879561#2879561">an answer</a> how to syntactically solve that problem. However, I want to point out two other arguments regarding this issue: </p> <ol> <li><p>No matter whether you're employing a <em>using directive</em> (<code>using namespace std</code>) or its lesser evil sister, a <em>using declaration</em> (<code>using std::cout</code>), overloading might lead to nasty surprises. It's not much hassle to type <code>std::</code> compared to <strong><em>spending half a night debugging</em></strong> to find out <a href="https://stackoverflow.com/questions/2712076/how-to-use-iterator-in-c/2712125#2712125">your code called <code>std::distance()</code> instead of your own <code>distance()</code> function</a>, just because you made a small mistake and <code>std::distance()</code> accidentally is a better match. </p></li> <li><p>A line of code <strong><em>gets written once</em></strong>, but - depending on its lifetime - <strong><em>it is read tens, hundreds, and some even thousands of times</em></strong>. So <em>the time it takes to write a line of code simply doesn't matter at all</em>, <strong><em>important is only the time it takes to read and interpret a line of code</em></strong>. Even if it takes three times as long to write a line with all the proper <code>std::</code> in place, if it makes reading it only 10% faster, it is still worth the trouble.<br> So the important question is: <em>Is it easier to read and interpret a line of code with all the <code>std::</code> in place or is it harder</em>? From <a href="https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c/1453605#1453605">another answer</a>: </p> <blockquote> <p>Here's one more data point: Many, many years ago, I also used to find it annoying having to prefix everything from the standard library with <code>std::</code>. Then I worked in a project where it was decided at the start that both <code>using</code> directives and declarations are banned except for function scopes. Guess what? It took most of us very few weeks to get to used to write the prefix and after a few more weeks most of us even agreed that it actually made the code <em>more readable</em>. (There's a reason for that: <strong><em>Whether you like shorter or longer prose is subjective, but the prefixes objectively add clarity to the code.</em></strong> Not only the compiler, but you, too, find it easier to see which identifier is referred to.) </p> <p>In a decade, that project grew to have several million lines of code. Since these discussions come up again and again, I once was curious how often the (allowed) function-scope <code>using</code> actually was used in the project. I grep'd the sources for it and only found one or two dozen places where it was used. To me this indicates that, <strong><em>once tried, developers didn't find <code>std::</code> painful enough</em></strong> to employ using directives even once every 100kLoC <em>even where it was allowed to be used</em>. </p> </blockquote> <p>I think it's sad that every book and tutorial you'll find skips <code>std::</code>, because that makes people getting used to read the code that way. When I taught C++ for several years (after the above mentioned experience), I told my students that I don't want to see any <code>using</code> directive or declaration in their code. (The only exception to that rule is <code>using std::swap</code>, BTW, which you'll need in order to have <code>swap(a,b)</code> pick up overloads outside of namespace <code>std</code>.) Once they got used to it, they didn't mind and, when asked about it, they said they find code without the <code>std::</code> prefix confusing. <strong><em>Some even added the <code>std::</code> prefix to code they typed from a book or tutorial which didn't have it</em></strong>. </p></li> </ol> <p>Bottom line: What's so hard about typing <code>std::</code> that everybody gets so worked up about it? By now I have been doing it for >15 years, and I don't miss <code>using</code> at all. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload