Writing pure code without using third-party header files [closed] - c++

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 8 years ago.
Improve this question
As try to learn C/C++, I'm always finding it frustrating that I need to use the header files. It makes it seem like it's not my code, but I am using some other person's code instead. I simply want it to be pure, and be written by myself without using the header files.
I know for certain that C/C++ includes libraries that can give the developer some functions in order to for example create a vector. The Boost library are similar to that, but again, I want to write my own code, and maybe create my own library for my work.
But is this possible? If I wrote my own header files for C/C++ that almost acted like the iostream.h file for example, only that I've made it my own, and optimized it, will it be beneficial for my applications/projects, or should I just use the standard library that is included with the programming languages?

My answer comes, at least partially, in the form of a rhetorical question:
Are you also going to write your own compiler?
You're always using something that someone else wrote, and for general-purpose use this is a very, very good thing. Why? Because they are the experts in their field, because they are multiple people, and because their code has gone through decades of rigorous peer review, thorough testing by millions upon millions of people, and many iterations of improved versions.
Shying away from that as an instinct is one thing, but refusing to use standard headers due to it is quite another, especially when you draw the line so arbitrarily†.
In short, there is a damned good reason why the C++ standard defines the standard library and why your compiler vendor ships an implementation of it. My strong recommendation is that you work to that principle.
† …which is why mine is not a "slippery slope" argument!

Off course you should use the standard library. The only reasons not do so are:
The class you want does not exist.
You are a pro C++ programmer and something about its implementation really annoys you.
You as a beginner want to learn something by trying to build your own simple data storage types (like for instance any vector type )
Your thoughts about "all should be made by yourself" are not that uncommon, but once you've implemented one of the standard types and have spent hours on it while your actual project hasn't progressed one line and when your new "own" type still misses half of the functionality - Then you'll realize that using an existing library (especially the standard library or well known others like boost) might actually be a clever thing.

It makes it seem like it's not my code, but I am using some other person's code instead.
How would you write the <fstream> library? Opening files is not something that can be done in the pure C++ language. A library that provides that capability is needed. At base, opening files has to be done by the operating system and the OS exposes that to your code. The OS itself has to deal with other software that enables it to do these things.
Or what about this: Addition doesn't happen by magic, so somebody had to spell out exactly how to do it for your program to be able to do a + b. Does writing a + b make you feel like you're using other people's code, the code which describes how the add instruction is implemented on the CPU?
No single piece of software is going to do everything. Every piece of software will have to interact with other components, and virtually always some of those other components will be the results of someone else's work. You should just get used to the idea that your software has its own area of responsibility and it will rely on others to handle other things.

Re-inventing the wheel is a bad idea. Especially if that wheel has been designed and built by people smarter and more knowledgeable by than you and is known to everyone else who is trying to build cars (program C++).
Use the headers; don't be daft.

By the time one re-implements most standard routines, one might as well make a new language. That why we has a wide selection of languages from which to choose. Folks have dreamed-up a better idea. Re-inventing the wheel is good - we don't drive on chariot tires much these days.
C and C++ may not be the greatest, but with 40 years of history, they do have holding power (and lots of baggage). So if you are going to program in a language - use its resources, with its strengths and weaknesses. Chances are far greater, your solution, will not be better than the existing libraries improved 1,000s of others.
But who knows - you code may be better.

Related

How do you parse or save data from or to json files with c++? [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 8 months ago.
Improve this question
I'm currently trying to gain some experience in coding with c++. I've already done some projects in other languages such as c# and other but I quickly realized that stuff is done quite different and that I got a lot to learn before I can start programming some more advanced projects.
However, I'm quite experienced in reading and writing data storing file formats such as json or csv (I already succeeded in writing to a csv file in c++ tho). But as it turns out it seems like you can't just work with json files in c++ as easy as you may know it from other programming languages.
I'd like to know if there are more common ways to create some easily accessable storage files that work in a similar way?
(For better understanding:)
As I already mentioned, I am aware that c++ is quite different from c# and a lot more difficult to learn because of its advanced syntax. I also worked on a beginning Project (which I thought would be possible to code) for a few hours where I want to log into some "accounts" whose (encrypted) login credentials are stored in a Json file (so I don't really think a code sample is necessary in this case). On the Internet I could find a lot of tutorials teaching how to work with simple text files, but I don't think that's gonna work for me because I plan to work with structured information formats that are easy to navigate. After watching a few tutorials and reading a lot of stackoverflow pages, I decided to ask myself. Thanks for your Help!
The reason you perceive other languages "simpler" to work with JSON is because those languages have built in class libraries to deal with JSON or they provide a near seamless dependency management framework that lets you very easily choose a 3rd party class library for the task.
The C++ standard makes very little assumptions of the environment that will run the code and thus has a much more limited set of "built in" libraries. And even the word "built in" is a bit of a stretch in the case of C++, since you can make a perfectly decent C++ compiler without providing most of the standard template library. In managed languages like C# or Java we tend to blur the line between "language", "class libraries" and "runtime" and even the build system, so when we encounter a language that has a much clearer distinction between these concepts, we are surprised.
Having said that, googling around a bit, you will find plenty of json parsing libraries for C++.
I recommend trying out Niels Lohmann's json library. I have absolutely NO intention on implying that it is the "best" or "fastest" or "easiest" or anything of the sorts.
Why I would recommend it is that you only have to include the header file and don't have to install and link to dlls, static or dynamic libraries, etc... Which is convenient if you only want a simple project without getting involved more deeply in building and linking and so on. But as I said earlier there are other solutions as well.

Should I make my code dependent on external libraries? [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
Tl;dr: Stick to the bold text.
Libraries provided by others can save a lot of programming time, because one does not have to solve problems that others already did. Furthermore, they often perform certain tasks much more efficient than one could ever achieve by oneself.
On the downside one adds a dependency to the program which can cause problems with licensing, compiling on other machines and so on. Also developments in the libraries may interfere with developments in ones own code. In extreme cases one is after some time restricted to the functionalities the library provides meaning that even a small extension might require the programmer to rewrite half of the library. In such a case one may rather want to exchange the library with a different one.
At this point one can be in big trouble if the whole code is cluttered with calls to the library. To prevent problems like this one can right from the start write a wrapper around the external library so that a library change reduces to changing the wrapper and no other code needs to be touched - in theory.
In practice, however, the interfaces through which the wrapper is called may not be compatible with the "new" library. Also a library might use data structures that are not directly compatible with the data types in ones own program. Then data needs to be reorganized and probably a lot of copying happens which was not necessary before.
Questions:
How can I avoid trouble with changing libraries?
Should I always wrap the functions external libraries provide?
Should I wrap data objects of external libraries as well?
Or should I instead completely decide for a library and stick with it?
Example:
I work on a huge program in which problems of linear algebra are ubiquitous. Recently, we started to switch to Eigen, an efficient library with broad linear algebra functionalities. Eigen comes with its own data objects. Now there are tons of std::vector<double> objects present in the code which would need to be replaced with Eigen's VectorXd to be able to nicely work with Eigen. It would be a hell of a work to do all these replacements. However, it would probably be even more work to undo these changes if Eigen at some points turns out to be not the ideal solution. Now I'm not sure whether writing an own vector class which just wraps the Eigen data type would actually reduce the effort if the library will be exchanged someday, or whether I will just produce more problems that way.
A couple of suggestions:
Pick a library used by big well established projects. Library authors can only play with the API as long as they have a small user base. A library like SQLite, which is used in huge projects (like Firefox) simply can't afford to annoy its users by making incompatible changes.
Pick an open-source library if you can (LGPL if your project is closed-source). That way, if the library development stops or diverges from your goals, you can fork the last compatible version and keep adding necessary features to the library yourself. It will be much cheaper than switching to a different library half way through.
Wrapping library types will do little help. Switching libraries is not painful because of type changes, but because the whole program structure may need to be adapted to the new library.

Why do people seem to insinuate I would rather not use Boost? [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
Very often here on SO I see notes about boost such as
If you are fine with using Boost...
or
If you can use Boost...
And I wonder, what's that all about? What should I be weary of? When can't I use boost? What are the reasons not to use boost? In my opinion boost is a great extension to STL; sometimes very heavyweight and clumsy, but great nevertheless.
I am not really asking for opinions about boost as such. I am rather looking for some concrete examples when should I think twice before using boost.
When I can't use boost? In my opinion boost is great extension to STL,
sometimes very heavyweight and clumsy, but great nevertheless.
Boost is not a library but a collection of largely independent libraries of individual quality. With this in mind, and also taking into account that I'm personally a big fan of most of Boost, here are some reasons I can think of for not using certain Boost libraries:
Some Boost libraries are redundant since C++11.
Some libraries are not widely used and thus require expert knowledge in your project which might be expensive to replace when an employee leaves the company.
Company guidelines which developers have to obey more for political than for technical reasons.
You have no guarantee that any Boost library will be continued to be maintained in the future. Standard C++ code written for some compiler today will very likely continue to work fine with a newer compiler by the same vendor 10 years from now, for simple commercial reasons. With Boost, you have to hope that enough competent people will have any interest in long-term maintenance.
No Boost library is documented as extensively, with so much material in countless books and on the internet, as the C++ standard library. Who will support you if you have some really exotic problem with a particular library? Surely with standard C++ your chances of finding people with the same problem (and existing solutions for the problem) are much higher.
Debugging some Boost code can be more difficult than debugging code that uses the standard library.
Because it's not an extension to the C++ Standard Library (nor to the STL, naturally).
It is a third-party distribution, that you must download and install, locally and (for some Boost libraries, if you dynamically link) on the target system. You must manage and document the dependency.
I shan't enumerate all the scenarios in which this is not feasible, but it should be self-evident that you cannot always use non-standard code. Not everybody is working on a platform on which you can simply write yum install boost-devel, write your code and move on. The world of computers goes far beyond commodity desktop PCs.
That being said, most arguments for avoiding Boost are incredibly weak, due to its extreme portability, and the fact that the majority of Boost libraries are header-only (which reduces the packaging overhead significantly).
Seems like a lot of fuzz for nothing
I don't think writing the phrase "if you can use Boost" can be honestly described as "a lot of [fuss]".
Maintenance mostly.
Once you add boost, you have to maintain it. Either get updates (and maintain any changes mandating changes in your code), or freeze the version and fix bugs yourself.
Both are expensive and backloaded costs. For a project with a lifespan measured in decades, such costs are highly important.
In addition to #LightnessRacesInOrbit's point, I'd say there are a few reasons:
Boost has a lot of code in .h and .hpp files, which you need to include in every translation unit (which uses the relevant parts of Boost), and those files are laden with complex and recursive macro use and smart - but again, complex - use of templates. The combination makes your compilation a w-h-o-l-e lot slower.
Boost isn't installed everywhere by default, so it's not always available to you just because C++ and the standard C++ library is.
(A new reason actually) A sizeable fraction of Boost functionality has made it into C++11 (more is in C++14, and still more in C++17). So, by now, there are alternatives in the standard library or even the language itself for part of what Boost offers.

Why is there a networking library proposal for C++14/17? [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 8 years ago.
Improve this question
Even though TCP/UDP/IP are commonly used protocols, I do not understand why they want it to be part of the ISO C++ Standard. These have nothing to do with the core of the language. Data structures are universally required tools hence STL makes sense but these protocols are too specific IMO.
There has been a long-standing sentiment that the C++ library's tiny focus area is something bad that's holding the language back. Most "modern" languages come with large framework libraries which include networking, graphics and JSON. By contrast, if you want to do any of these in C++, you a) don't get anything by default, and b) are overwhelmed with a choice of third-party libraries which you are usually unable to appraise properly and select from.
This is how that side of the opinion goes.
Of course there are other people who think that that's just the way it should be. Nonetheless, standardization is hard work, and whereas languages like Java and C# and Go have large companies behind them that can put energy into developing a huge library, C++ doesn't have that kind of manpower, and most people who spend time on C++ standardization are more interested in core aspects of programming: data structures, concurrency, language evolution (concepts, ranges, modules...).
So it isn't so much that people are generally opposed to a larger library, but it's not a priority for many. But if good ideas come around, they have a good chance for being considered. And large library components like networking won't be going into the standard library anyway, but rather into a free-standing Technical Specification, which is a way to see whether the idea is useful, popular and correct. Only if a TS gets widely used and receives lots of positive feedback will there be a possible future effort to include it into the IS.
(You may have noticed similar efforts to create Technical Specifications for file systems and for graphics.)
C++ 11 includes Threading in Standard. Now programmers need not to write PThread in Linux and Windows Threads in Windows, separably. Same can happen if Networking library gets Standardized.

How important is Boost to learn for C++ developers? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am curious to learn Boost. But I wanted to ask:
How important is it to make the effort to learn Boost?
What prerequisites should one have before jumping on Boost?
Why I am curious to know about Boost is that many people are talking about Boost on IRC's channels and here in StackOverflow.
I think anyone that is seriously considering C++ development as a career should learn Boost, and learn it well. Once you get into serious programming you will realize how beneficial these libraries can be and how much more productive they can make you. Not only are they cross-platform, but once you get into data crunching with large numbers, the math libraries especially will seem like a gift from above.
As a game developer, I've been shocked by how many people don't know about Boost. I've mentioned it to contacts in various game studios and not only is it frequently not used (is licensing or porting it a problem?) but many people haven't even heard of it. This leads me to believe that from a career perspective, it's not actually critical to learn Boost, but from a personal development standpoint, it is definitely vital. :)
Discussed previously: what are the advantages of using the c++ boost libraries.
As for any pre-requisites, you should be familiar with the STL and some experience of templates wouldn't hurt. I'm always amazed at how many C++ programmers don't actually use either the STL or templates.
It's very important, especially as many libraries from Boost are getting into the C++ standard -- by using Boost, you get an early look at how the standard will look like (shared_ptr, anyone?).
Moreover, even if you don't use them, the Boost libraries are very well written and often interesting to look at; they do some really advanced C++.
I feel that boost is such a productivity enhancer that I don't think I would accept a job with a C++ shop not using boost.
A language is a tool. Use it if it helps you accomplish something.
I am so sick of these religions. "Should I use Boost? If I don't use Boost, does that mean I'm not a real C++ programmer? Will other C++ programmers like me?" Please. Any C++ programmer who cares if you use Boost or any other library is a close-minded jerk, and you should have nothing to do with him.
Rather, go find an intelligent, open-minded person who can tell you how Boost and who-knows-what-other library has helped him in his own work. He'll even admit that sometimes you don't need those libraries.
Alternate answer: re-implementing part of Boost or STL yourself is a good way to keep your programming abilities sharp. In other words, a C++ programmer who can't fall back to C because he's without his libraries is a weak programmer.
Boost has rich set of libraries that you get it for free.Get to know what are all the libraries available in boost so that you can use one if there is a need.About learning ,select libraries that are included in c++0X so that you can use it and soon compilers are going to support.About particular library learn it when ever you need.
Judging (scientifically :-) by the huge quantity of questions on SO about C++ which have top-rated answers along the lines of "Use Boost::SomethingOrOther", I would say it's very important.
The thing that drew me from C to Java instead of C++, was the huge quantity of supplied classes in Java. Boost almost manages to convince me to go back, except for the fact that I'm now heavily mired in web services where Java is the lingua franca.
Please remember boost is just a set of libraries which can be used to improve productivity (stop reinventing the wheel).
They are, by all accounts, well written and use techniques that you might not (i.e. defintely won't) think up by yourself. If your intention is to look through the source to learn advanced c++ techniques then knock yourself out but I think I'd buy a good book instead.
On the other hand, if you just want to use some library functions to improve your productivity consider your options. What are you developing and so what sort of libraries do you need?
Our company has cross platform products that use boost extensively but we also have windows only products that use some boost but, for the most part, rely on microsoft's libraries. MS libraries are good quality and have (imo) excellent documentation (part of MS success lies in making windows as easy to program as possible for third party developers). I mention MS specifically as they offer a broad range of libraries for many purposes like boost. We also use numerous other more focused 3rd party libraries (i.e. libraries that provide functionality in one area such as cryptography).