In C++, what is the difference between namespaces and libraries? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am extremely new to C++, what is the difference between the C++ Standard Library and the std namespace. I get them confused and don't know when to use which. Also how does the header files correlate with all this?

The C++ standard [template] library is a collection of tools - specifically containers and algorithms - that are useful in a general sense. It is particularly powerful when the algorithms are applied to the containers - when sorting a vector, for example.
Namespaces serve to decorate the symbols defined within them and thus avoid collisions between symbols defined (usually) by different libraries. All standard library code is in namespace std. Thus we have, for example, std::vector, std::sort and a whole lot more.
To pick another library at random, the popular Nlohmann JSON library defines all its symbols in namespace nlohmann, and in the unlikely event that this defined a vector class, it would not clash with the standard library.
You will often see people on SO (and elsewhere) coding using namespace std;, sometimes in a header file, and perhaps you now understand why this practise is not wise.
Does any of that help at all?

Related

Why is Boost.ProgramOptions not header-only? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Some boost libraries are header-only, some are not, and for various reasons etc.
Is there a specific reason/design decision why Boost.ProgramOptions is not header-only?
I'm wondering because it claims to be a "small" library in its the documentation and I don't see any system-related reason (like threads or asio).
Program Options claims to be small, but it turns out to be the second largest library we were building, after Regex. (It is bigger than boost Filesystem and Thread libraries.) I believe that you should be glad they're building a library for it instead of choking your project with a ton of included headers. Perhaps the author thought it would be small when he started and forgot to change the comment when it continued to grow and add features.
Not all C++ code can be written in just headers due to one-definition-rule violations.
For example, the storage reservation for a static member of a class needs to be in exactly one translation unit (although future C++ standards may obviate that).
The original intention was for Boost to be header only, but they had to quickly relinquish that aspiration.

Can the C++ Keywords be used with just only the iostream include directive? What else is there besides keywords that can be used with only iostream? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I consider myself a C++ beginner and have come to a question when dreading the fact of using many include directives. So instead of figuring it out myself the long way, I have come to get some help before I can learn and practice more.
From the book Absolute C++ 5th edition by Walter Savitch, says that "The operator sizeof is part of the core C++ Language and requires no include directive or using directive." 12.4 Random Access to Files, page 585
I want to know where can I find other operators or codes that does not use the include directive or using directive.
This could help me build programs before I can start learning more of the C++ Libraries.
Thank you!
What you are asking about can all be answered by going on cppreference. Basically there are many default libraries that can be included by writing #include <lib>. So for example you want to use vectors (well optimized dynamic array), you write #include <vector>, if you want to handle input and output you write #include <iostream>, etc.
All of this can be found documented on page I've linked above. If you don't know with what to start some basic and most libraries would be iostream, string, vector, map.
Then there is an memory library, which provides for example an unique pointer which is a really strong tool in C++.
Then there is an algorithm library, containing useful functions working with containers over iterators.
These are just examples that came to my mind first and I've used them the most. If you are not sure about anything you can always search on cppreference for full documentation on it.
If you have any other or more specific question feel free to ask.

C++ Iterating through files in a directory: Is it bad practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
The question has been asked many times here: "How do I iterate through files in a directory in c++?"
I've seen the responses, typically suggesting to use an external library (usually boost filesystem) to handle this and the OS differences in implementation.
My question is: why? Why is this so difficult?
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
Is it more appropriate to implement a single file solution and use another language to implement the file iteration?
Relative to other languages, C++ has a tiny standard library. This has its advantages (porting C++ to a new platform is much easier), but it also means that, to get a lot of things done, you're going to rely on external libraries.
Filesystem work has not, until C++17, been something that has been a part of the C++ standard. And even now, some people are resistant to the C++17 filesystem library because it doesn't work quite so well with certain types of underlying filesystems.
Note that many languages that have standard filesystem support don't support these platforms at all.
I'm new to the language and can't help but feel that I'm trying to overstep the bounds of idiomatic c++.
If you're going to use C++, then you need to accept that you're going to have to go out and use other libraries for a lot of the stuff that many other languages give you for free. Using a library is how you get things done in C++; it's not "overstepping the bounds" of anything.
You can't iterate the files in a directory in C++ without using platform-specific APIs. The core C++ language and the standard library do not provide any mechanism to obtain a directory listing and iterate through it. So in order to do this, you must use an external library or platform specific API.

PascalCase vs camelCase for functions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Which is the most common in C++? Is it different between member functions and free functions? Obviously the standard lib uses snake_case for everything, but almost nobody uses that.
A lot of people (including me) prefer the underscore_style. I think from my 20+ years of experience, currently working in a 100.000+ employees company, having reviewed other company's code, etc. I would expect the underscore style to be the most commonly used style. Why? The STL uses it and almost everyone uses the STL. Also, large parts of Boost use it. Of course, there is no way to prove this.
In some domains other style guides or habbits are in place with different naming conventions, but this might be misleading you if you are in such environment to think that it is also common in other places.
To answer your question about member- vs. free functions: I don't think there is a difference wrt the style used.
What I think is most common in C++ is:
Template parameters: They are usually PascalCase.
Macros: They are usually UPPERCASE_UNDERSCORE_STYLE.
Most everything else, including function-, method-, variable- and parameter-names are underscore style.
There are also studies about the underscore style, here's an except from a study from 2010:
Although, no difference was found between identifier styles with respect to accuracy, results indicate a significant improvement in time and lower visual effort with the underscore style.
These studies will IMHO lead to even more adoption of the underscore style in the future. But again, there is no way to prove this (or to predict the future).
It depends. If you're working alone, I'd recommend picking a standard and going with it. If there's some code you're interfacing with, it may make sense to follow that code style.
On projects without a defined style guide, I default to the Google C++ style guide (https://google.github.io/styleguide/cppguide.html).

Why some libraries must implement basic data structure? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Some open source libraries have tendency to re implement basic structures like string, list, stack, queue...
Why don't they use stl library? Is stl not good enough?
C++ as a language existed for many years before the STL was standardized and for several more years before the STL was implemented consistently across all compilers. Libraries that were originally written during that time may not have wanted to rely on the STL in order to be more portable.
Another reason related to portability is embedded use. Qt, for example, re-implements most of the STL because Qt applications are sometimes targeted at embedded platforms (e.g. Nokia smartphones) which may not have an STL implementation available.
Exposing STL types in headers can, in some cases, lead to nasty, nasty link times. On large projects, that can be sufficient reason to "hide" them behind a proprietary-looking API.
STL implementation differ on every platform, so exposing STL in library will have risk, for example if you expose std::map on your library, since you can not export std::map from your library, your library will be forced to use std::map implementation from your library users (the one that load your lib), this will cause some incompatibility such as different version of STL implementation, different STL allocator class, and some platform specific issues.
One of the reasons may be that STL containers are not intended to be used as base classes - STL design issues Wikipedia
To STL or NOT STL that is the question - SO