can I think framework as class template in c++? [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 3 years ago.
Improve this question
After reading some posts online, even though they explain it using non-coding way, I still feel unclear about this. They explain it using some examples in our real life.
Could you please explain it in coding way?
What is framework?
My understand is that it is pretty much like class template in c++.
template <typename T> class Reader {
// code details
}
So, if we want the reader to read newspaper, we instantiate Reader<NewsPaper> or Reader<ScaryBook>
Am I right? Or what's wrong if understanding framework this way?
Thank you!

As for me term "framework" doesn't suit C/C++ world well. Traditionally external modules in these languages are named "libraries" or "libs" (this naming is used in *nix systems as well when downloading them from web repository). Even though there might be some different interpretation for this terms, they are generally about the same thing (especially for C/C++ applications).
External library is a piece of code which performs some functionality. When dealing with big libraries (like one for creating, networking, image processing, multi-threading etc.) programmer has to follow some workflow proposed by it. You may think of library as a "black box". It provides you interface (rules how to use it: input and output params, error codes etc.) but you don't know what is inside and generally don't need it.
Depending on solved problem library may be (or not) extensible. Library for reading various audio formats may not provide way to create readers for new audio formats but networking library should provide functionality to create user-defined networking protocols.
And template is a variadic piece of code. One backbone code which suits different datatypes. It's a powerful tool both for applications design and performance (which is commonly used in libraries as well for that reasons).

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.

How to program an interchangeable library? [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'm currently programming a small library with abstract classes. Its a library that offers simple GUI creation classes, like widget, main_window etc.
My goal is to code an application using interfaces and factory functions to creates actual objects. I use cmake for my project (for info but doesnt really matters). I'd like to include in my lib the abstract classes (easy) and different engine (like qt5_engine, gtk_engine, ...).
The client(my application) using the lib can see only the abstract interfaces, and factory functions(methods, lambdas, builder classes??). When building the lib, I choose which engine to build (only one) to get libmy_lib.so. My application will use libmy_lib.so, no matter if its compiled with my qt5_engine or gtk_engine (staticly linked into my_lib for each engine)
So that finally, I can simply overwrite libmy_lib.so compiled with a different engine to change the GUI used by my application. But I also like to know of a clean way to implement such lib with different engine, to get a clean code, the 'best pratices' for lib programming in other word.
I kinda found the way to ask it, and so, the way to search for it >< I accepted an answer which is the part for the 'change le lib file to change the GUI' but if you have references to library coding the clean way, the modern way, that would be awesome...
Thx
Look up "ABI compatibility". The Qt/KDE projects have a good guide about that:
https://wiki.qt.io/Qt-Version-Compatibility
https://community.kde.org/Policies/Binary_Compatibility_Issues_With_C++
The extreme version of that would be the "Hourglass Interface" pattern, where the binary interface of the library is pure C, but there's an inlined C++ API above it and a C++ implementation below:
https://www.youtube.com/watch?v=PVYdHDm0q6Y
EDIT: Just wanted to add a clarification: Where these guides talk about compatibility between different versions, your libraries would have to adhere to the same restrictions for different backends.

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 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.

Writing pure code without using third-party header files [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 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.