using namespace std
is for noobs.
|
|
std::cout
and std::endl;
in the whole program text. Is is still a nooby style?
ninja01 wrote: | |||
---|---|---|---|
Ok good to know that using using namespace std is for noobs.what about:
std::cout and std::endl; in the whole program text. Is is still a nooby style? |
In order to avoid naming conflicts with other code (remember new names are added in each new version of C++) all names in the standard library (except for a few exceptions) are part of the std namespace. That's why you have to write std::cout, std::cin, etc. You can do using std::cout; and then you can write cout without std::. This is fine from a name clashing point of view, but in my opinion it's not worth it. You would have to maintain lists of such names everywhere and have to think what names are in the list, should I add this name to the list, is this name still in use or can I remove it from the list? For me it's just easier to write std:: everywhere. I actually think std:: makes the code easier to read. It makes it clear what names are from the standard library. This is especially true if I read someone else's code because I don't have to wonder if for example max(a, b) calls std::max or if it's some function that the author wrote himself. I can understand if you prefer to use using namespace std; and I don't think it's the end of the world if you do use it. Beginners often use the standard library on almost every line and have few own functions and classes to confuse it with. However, in a larger program the standard library names are often less dominant, with lots of functions and types that have been created in the project or come from libraries other than the standard library, so std:: becomes less of a distraction and more of a useful info tag. |
ninja01 wrote: |
---|
what is a flush? |
This is often better for performance and for the terminal... |
jonnin wrote: |
---|
the majority of projects and programs are going to have a GUI for most coders these days |
Duthomhas wrote: |
---|
Console is just another file device, and you will always need to know how to read from and write to file. And many, many utilities still use stdin/stdout as their primary I/O — especially transformational utility programs. Knowing how to manage an I/O stream is never going to go out of style. |
2. They say that only noobs use using namespace std; |
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice There are possible risks involved with having the using namespace std; directive in your code, risks that usually are not mentioned when learning C++.C++ Core Guidelines, SF6 & SF7: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using Read the explanatory text before and after the SF6 sample. Having the using namespace std; directive in code is not an error. The people who have it in their code presumably know the risks. Do you know what can happen?It was originally supposed to be used with older C++ code that didn't have standard library namespaces and make it easier to transition that legacy code to newer C++ language versions. There are now safer C++ ways that are less likely to have risks, using declarations. https://www.learncpp.com/cpp-tutorial/using-declarations-and-using-directives/ There are a few select features of the C++ stdlib that require a using declaration to activate the desired feature. {need an example, fer shure} A popular 3rd party library, Boost, is multiple namespace structured that not having namespace declarations for the library is a pain with lots of repetitive typing that can get mistyped. The C++ Core Guidelines are suggestions, not "you must do this" requirements. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#SS-aims Personally I never have the using directive in my code, ever. I prefer to prefix std:: when using C++ stdlib features. Now it is automatic typing std::. I actually have to stop and think to NOT use it. I do have using declarations on occasions, though I try to use them very sparingly. On a case by case basis, mostly when using a C++/3rd party library feature buried in a namespace morass. |
5. Macro is evil! Why? |
3. They say that using std::endl is a bad practice. Use instead \n. Why? |
std::cout << "Hello World!" << std::endl;
std::cout << "Hello World!\n";
Many standard library functions (for example, many algorithms) expect their arguments to satisfy Swappable, which means that any time the standard library performs a swap, it uses the equivalent of using std::swap; swap(t, u); https://en.cppreference.com/w/cpp/named_req/Swappable |
using std::swap; swap(x,y);
is a newbie level mistake.std::literals::string_literals::operator""s
:std::literals::string_view_literals::operator""sv
the example I would present for a using declaration is std::literals::string_literals::operator""s: The same "a using declaration works better" with std::literals::string_view_literals::operator""sv |