4.6.0
Freundlich's C++ toolkit
|
Freundlich's C++ Toolkit (fcppt) is a collection of libraries that aim to improve general C++ code through better typing and functional programming.
The source code and releases are available on github.
See Installation and Usage on how to install fcppt and on how to use it in your project.
Avoiding partial functions (functions that do not terminate, i.e. crash, on certain inputs) is one of fcppt's main goals. These are, unfortunately, all over the place in common C or C++ code: std::unique_ptr
can be null, operator[]
of std::vector
can be used for out-of-bounds access, iterators can be incremented past their end, and so on. Partial functions place a huge burden on the programmer since they have to ensure that no code path is taken that actually calls a function with invalid arguments. Consider the following example, where we avoid partial functions:
The fcppt::container::at_optional function returns an optional reference into the container, which the fcppt::optional::maybe function uses to either print the value or print that no element at position 2
exists. Notice we do not ask explicitly if the optional has a value or dereference it (which can crash if the value is null).
Explicit loops are another source of mistakes: The loop not only specifies what to loop over but also how the result is formed. What to loop over is best expressed using ranges, while forming the result can be expressed using special algorithms. A very common operation is mapping one container into another. Here, we want to convert a container of integers into a container of strings by using fcppt::output_to_std_string, which converts any type into a string that has an output operator defined:
Code using a standard loop would allocate an empty vector and then use push_back
repeatedly. For good performance, it should also call reserve
before the first push_back
. All of this is nicely encapsulated in fcppt::algorithm::map, so that you only have to focus on how to transform a single element into another.
This also brings us to the next point, which is initialization. It is important to directly initialize objects and declare them as const
whenever possible. Obviously you cannot do that with loops that change their result in each iteration, like by calling push_back
. There are, however, even more complicated cases in which direct initialization becomes hard to express: Consider an array that should be initialized depending on its size. For simplicity, let us initialize each element with its index:
Notice that the parameter is actually an integral constant, which we use to access the make_value
template.
To force initialization, fcppt also replaces certain standard types with types that have no default constructor, for example fcppt::unique_ptr, fcppt::function and fcppt::variant::object.
Following is a list of a few other libraries (for a complete list, see the modules page at the top):
optparse-applicative
library. For more technical details, you can also take a look at our Coding style.
The documentation is split into the following parts: