+ 2

why do we use using namespace std; ?

21st Jun 2016, 4:38 AM
Tushar Chetan
Tushar Chetan - avatar
5 Respostas
0
"using namespace std;" imports, i.e "makes available" all functions, structs and classes etc from the namespace std in the namespace the line is written in (in all cases here at SoloLearn the default namespace). Otherwise you would have to write "std::" before every element of the std namespace.
21st Jun 2016, 9:17 AM
Stefan
Stefan - avatar
0
Btw, the std namespace is the namespace of the standard library in C++ and is therefore declared when files like cstdlib or string are included.
21st Jun 2016, 9:19 AM
Stefan
Stefan - avatar
0
This has nothing to do with "old" in my opinion, as this is an approach chosen for a specific purpose. As C++ is a statically typed language it requires explicit management of variable types etc. This explicit approach is also kept for namespaces. It forces to either import the namespace or use the fully qualified name of the class / struct / constant etc. There's a reason why big software systems are mostly built using statically typed languages (like C++ or Java). This explicit handling of types enables the compiler to check for errors at compile time which is not possible for dynamically typed languages (like JavaScript or python). Also explicit type management is the cause why C++ programs run so fast as it enables code optimizations that will never be available to dynamically typed languages.
21st Jun 2016, 11:16 AM
Stefan
Stefan - avatar
0
that's a very acceptable opinion. just have the feeling that interpreted languages are in a sense newer than compiled and the more intuitive I think the better :)
21st Jun 2016, 7:32 PM
Klodian Lula
Klodian Lula - avatar
0
You are right Klodian, the dynamic typing approach has gotten more attention in recent years. Over time it became obvious that the manual work required to handle the formal overhead of static typing, prevents people from actually writing their business code. The dynamically typed or untyped languages try to fill this gap. Unfortunately, it seems to turn out that it is still hard to handle big software projects efficiently. I have never seen and heard from software consultant friends that a big software project with a dynamically typed language turned out to be as easy and successful as the word "intuitive" might suggest. Dynamically typed languages fill what I call the "prototyping gap" as they allow for quick iterations of the code (as might Excel or Mathlab for certain scenarios). Still, dynamic typing has to enforce a certain level of type correctness as there are types for which an implicit conversation does not exist. Also, and that is more of a problem in my opinion, there are implicit type conversions that do not make sense in the specific context of the code and will still be done by the interpreter (leads to annoying errors that can only be discovered at runtime). This is why proper handling of types is also required in dynamically typed languages. The only problem is that there's no nagging compiler that prevents you from making these mistakes. In turn this requires more testing effort than in a statically typed language (as to more direct development time). The newer standards of C++, such as C++11 and C++14 try to ease the manual handling of the static typing with concepts as auto type deduction. Therefore, my answer is merely directed to prevent the impression that the static typing concept is outdated. It is not. It fulfills a different purpose and that means there are good reasons for its continued existence (also as they started to address the overhead problem as shown for C++11 and above).
22nd Jun 2016, 3:51 PM
Stefan
Stefan - avatar