FCPPT: Introduction

FCPPT: Introduction


FCPPT (Freundlich's C++ Toolkit) can
also be known as a collection of different libraries that enhance a user's
program by using functional programming.



One of the main priorities of
fcppt is to ensure to reduce or not use partial functions. Partial functions
are those functions which go in loop or unable to end, sometimes crashes on
certain type of inputs. But unluckily these are very common in C or C++
programming. For example, std::unique_ptr can be written for null, the operators
[] that is used in std::vector is for out-of-bound access, sometimes we use
incrementors even after they have ended and there are many more. For coders or
developers these partial functions create a huge problem as they must check
regularly that no procedure or way is taken by code which finally results in
calling a wrong argument.


Let’s take an example of a
particular code where we do not use partial functions:

void print_at_1(std::vector<int>
const &_vector)

{ //Using fcppt to
enhance the code

  fcppt::optional::reference<int const>
ref{fcppt::container::at_optional(_vector, 1)};




      [] { std::cout << "1st
position has no value"; },

      [](fcppt::reference<int const> _val)

        std::cout << "Value:  " << _val.get() ;

       //After execution we can observe the




fcppt::container::at_optional function gives us an optional reference for the
container to be used, and that fcppt::optional::maybe function makes use of to
print the solution or show that there is no value at 1st position.



Well, another place of errors is
explicit loops. The loop specifically tells us what to loop in program and how
the desired solution is achieved. The ranges are the best way to tell what to
loop over, while specific algorithms indicate the way to formation of results.
We map one container on to another one.


Let’s take an example where we
want to change a container of integers into container of strings by the help of
fcppt::output_to_std_string :

int_vector_to_string_vector(std::vector<int> &&_vector)

{ //Algorithms and
fcppt used to change from integers to string


      std::move(_vector), [](int const _value)
{ return fcppt::output_to_std_string(_value); });



We needed an empty
vector, so we used a standard loop and then used push_back repeatedly. For
enhancing the program more, we need to use reserve before using push_back for
first time. All this information is nicely contained in fcppt::algorithm::map,
so it helps us to divert our mind to only changing a single element to another.



Directly initializing
objects and declaring constants wherever necessary is very important. But its
not possible to that in certain loops where we achieve different results after
every iteration, like in push_back. Even more tricky cases are there in which
direct initialization is very difficult to show, like initializing an array
depending on its size. So, for ease let’s initialize each element with its

template <std::size_x

struct make_value


  static constexpr unsigned int const val =
fcppt::cast::size<unsigned int>(A);



template <std::size_x

int, A> init_array()

{ // Using parameter,
we use make_value template

fcppt::array::init<fcppt::array::object<unsigned int, A>>(

Index>(std::integral_constant<std::size_x, Index>)

      { return make_value<Index>::val;


We can make out that

parameter is an integral constant. With the help of that we access the
make_value template easily. Sometimes we must force initialization so fcppt
some types which has no default constructor with  the standard types. Like, fcppt::unique_ptr,
fcppt::function and fcppt::variant::object.