A long-last acquaintance

 

C++ in the modern world
Call it your friend, foe, nemesis, or any other adjective that you could assign, but the mere mention
of the programming language C++ evokes all kinds of feelings that a developer never realized can be
expressed. The best memory for a developer is formed in the early stage of their profession. We have seen
that this is true across cultures and countries. The best memory need not be always a good one always for
all, it could be a bad one or it could be something that you want to escape away from. The unavoidable fact
is C++ is part of that best memory. Once a developer steps into a professional environment, this language
often takes a back seat. Programming languages like C#, Java, JavaScript, HTML etc. takes a center
stage. You grow comfortable willingly or unwillingly to this new reality. For a few C++ still breathes into the
professional world. However, in most cases, we noticed they carry this notion that they should find new jobs
that offer them opportunities to work in non-C++ programming languages. Over time those notions grow on
them stronger to a level that the rationale evaluation of their programming career fades away. We
ourselves never gave much room in our minds to C++. Reasons for this are a spectrum. Ruminating on
them will take our focus from the intent of this dispatch. Yes, we fly around about and talk about status-quo
in the C++ world. We did not give much attention but the world around us is still built on C++ programming
language. It has evolved, maybe not at the same pace as you notice for Java, C# or JavaScript. The ISO
standard for C++ which many compiler targets and offers the benefits of portability is now called C++20.
Don't get swayed by the number 20. We do not think there is a natural association between the count of
versions when you learnt versus now. It is a string token. In all likelihood when you would have picked up
C++ it might be C++98. But what must be of interest and hopefully draws your awe is C++20 was voted into
a standard in 2020. The work for its successor started right in February of the same year. Thus, the version
that will be soon called recent is going to be called C++23. Pursuing the standard process for adopting
C++23 is currently open for internal voting on its adoption. Let this statement sink into your thoughts – the
C++ standard is evolving and is being defined in 2023. Like most of us if you have drifted to other
languages and use them in your professional lives for whatever reason your first programming language is
still alive and kicking well and wishfully remains like that.
Why did we ruminate on this for this long? By the way, the paragraph above is the rightful introduction to
our first programming language in the present time. A long-lost acquaintance.
To-do app
Long back when you started learning to program you would have started with "Hello World". It is so famous
that anyone bootstrapping a new concept; states they are doing a "Hello World" equivalent. Modern
programming languages and frameworks are pushing the "To-do" app to a similar status. Thus, we will re-
ignite the C++ using the To-do app. We will start with a console-based application and transform it into a
web-based app all using C++ through this series of interfaces.
We start with first the structure. The tooling to get it functional deserves its own space. Given all about a
To-do app is known too well we will not annotate much about it, right away we start with the header file for
the entity Item.

#include <string>
using namespace std;
namespace TodoStarter {
class Item {
public:
Item(string);
void markAsDone(string);
private:
string title;
bool done;
}
}
The reason we define the header file is more debatable. To date, the use of this practice is encouraged.
Not that we infer to say it is bad. We intend to remind the longevity of this practice. The practice of
separating the definition from implementation. This is the same code that you would have written when you
graduated with few lessons in C++ while in the university. C++20 aligns with the new notion, of the module.
With the header file, it was treated as a macro that replaces done before the compiler reads the code.
Module transforms the process and compiles them into binary. This compiled binary then can be
transported and used anywhere with solid implementation. Contrasted with string replacement in cpp file
before compilation, the definition of a type is now a first-class object and binary object itself. Let us now
look at how a module will look like for the above code –
#include <string>
using namespace std;
export module ToDo
namespace TodoStarter {
export class Item {
public:
Item(string newItem) {
title = newItem;
done = false;
}
void markAsDone() {
done = true;
}
private:
string title;
bool done;
}
}
Notice carefully along with using the namespace concept we have introduced a binary-oriented concept of
structuring code which aligns much with modern-style JavaScript and other languages. The notable
addition to erstwhile code is export [module name] and export class. More importantly the
implementation is already inlined without having to use the inline keyword. Let us not already worry about
how to consume it. We will bring that up in some time, but before that let us explore a few simpler yet
notable additions to C++20. Notice the subtle change below –

#include <string>
using namespace std;
export module ToDo
namespace TodoStarter {
export class Item {
string title;
[[no_unique_address]] bool done;
public:
Item(string) {

}
void markAsDone(string){

}
}
}

By using an attribute no_unique_address we are allowing the class to save 8-bytes in case the
variable done is not initialized. This attribute gives a window to control the memory usage for classes that
have attributes that are not yet initialized after the constructor has finished its job.