Using the std namespace and other alternatives

using namespace std;

      

So far, in computer science courses, that's all we've been told. Not only that, but everything that we are allowed to do, otherwise we will be punished for our code. I understand by looking at the code posted on the internet that you can use :: std or std :: to accomplish the same thing.

My question is why? Obviously for learners and simplicity, using a global declaration is simpler, but what are the back-shortcuts? Is it more realistic to expect :: std in the real world? And I suppose to add to that, what is the logic / concept behind the use declaration? None of this was explained during my courses and I would like to understand it better.

As a general question, if I haven't been looked into this content, vectors, templates, classes, or error handling, it looks like I'm missing out on a lot of essential C ++ features?

Thanks in advance!

+3


source to share


4 answers


This is really one of those things that you can discuss over beer for hours and hours, and still haven't received the answer that everyone is happy with.

If you almost always use the functionality std::

, adding using namespace std;

at the beginning of the file isn't too bad. On the other hand, if you are using things from more than one namespace (for example, writing the compiler with llvm::

as well as using std::

, there can be confusion as to which parts are part llvm

and which parts std::

- so in my compiler project I don't have not a single file with using namespace ...;

- instead I write llvm::

and std::

as needed.There are several functions (unwise, perhaps) called Type()

and in some places use something->Type()->Type()

to get what I need ... Yes, it sometimes confuses me even a little. ..



I also have many things that look like Constants::ConstDecl

and Token::RightParen

so I can quickly see what is. All of this MAY be made shorter and "simpler", but I prefer to see where things are in most cases.

Being more verbose helps make it easier to understand where things are - but it does for more typing and more reading, so it's a balance.

+3


source


I would say that in general you are not declaring the use of std globally. I think if you are making a simple application this would be enough. However, when you work in a large organization, you often have different namespaces that are used, and they may have overlapping objects. If you have a function in std and a namespace you created, and then "using namespace std" AND "using namespace yournamespace" is called, you will get unwanted results when you call that function. When you prefix each call with a namespace, it keeps it clearer and doesn't give overlap problems.



+1


source


common why?

Naming things is one of the most difficult aspects of software development. Beginners simply have no idea how their names can later create ambiguity.

In particular, our programming jargon has often preferred conditions for certain questions. These preferences can lead to the creation of unrelated instances of classes with the same (or similar) symbol with similar meanings.

Some of my commonly used symbols include init (), exec (), load (), store (), and I use timeStampGet () a lot of places. I also use open (), close (), send () / recv (), or write () / read ().

So, I could rename init () in each of the 3 namespaces and the 5 objects I added it to, but it's much easier to specify which one I want. I find exec () in 2 namespaces and 12 objects. And there are 3 different timeStampGet () methods that I am using. Whether namespaces or functions or class methods, these symbols make sense to me.

Also, I find 5 char "std ::" namespace-as-prefix completely natural and much preferable to globally "using std namespace". I suppose this happens with practice.

Another element is any where a larger namespace or class name gets tedious, I sometimes add the short typedef name ... here are some examples from production code:

typedef ALARM_HISTORY   ALM_HST;
typedef MONITOR_ITEM    MI
typedef BACKUP_CONTROL  BC;

      

On one team, we agreed to use well-defined "full" names, which sometimes became tiresome because of the length. Later in the project, we agreed that typedefs (for short classes or namespace names) could be used when they were simple and didn't add confusion.

+1


source


I personally hate "using" ads. For me, they make the code unreadable and you break up namespaces. I've spent 20 years as a maintenance programmer and I hate anything that makes the code harder to read - in my opinion the use is as useless as the cast specifications.

What's more readable is 6 months - a year - 10 years down the line.

UDP::Socket sock
sock.send(data)
TCP::Socket sock2
sock2.send(data)

      

against

using UDP;
using TCP;
sock.send(data)
sock2.end(data)

      

I also don't like namespace aliases

using namespace po = boost::program_options; 

      

Now you're making the next programmer more diligent with an extra layer of indirection by looking at how po compares to boost :: program_options. The same goes for those awful typedefs

typedef long QUADWORD;

      

What size is a square word - how long is 4 bytes long? 8 bytes, maybe 17 bytes in my OS

My last occupation is if you can't type then not be a programmer - saved keystroke! = Good maintainable code

+1


source







All Articles