Lambda expressions have been undoubtedly a celebrated addition to C++’s core language feature. Since its addition to C++ in the year 2011, it has gone through upgrades and evolution in the C++14 and C++17 standards. Today they are one of the most ubiquitous feature. No doubt they are first class citizens of C++ and still continue to evolve.

C++14 introduced the generic lambda expression into the core language feature which allowed the lambda expression to accept any parameter in its parameter list. C++20 has taken generic lambda expression to a next level whereby lambda expression allows template type parameter in…

In the first part of the three part tutorial series, we looked at the constinit specifier and learned how it is useful for getting a guarantee on initialization of variable at compile time. In this part, I explain the second specifier introduced as a part of the core language feature:consteval.

1. Introduction

In most simple terms, a consteval specifier, which can only be applied to functions guarantees that it produces a compile time constant. Failing to do so results in compilation error.

The cppreference page says the following about consteval specifier:

consteval - specifies that a function is an immediate function, that…

In this three part tutorial series, we explore the new specifier, constinit , and consteval added in C++20 as a core language feature and compare its subtle differences with constexpr specifier that is available since C++11.

In the first part I explain the constinit specifier. The most simple explaination of constinit is that it guarantees that the variable is initialized at compile time and when the initialization is not possible we get a compilation error.

1. What does constinit mean?

constinit specifier can be used only with static storage duration variables to and it guarantees compile time initialization of variables. Lets see this in action…

In part one till seven of the tutorial series, we looked at how to use the C++20’s three way comparison operator. In this part of the tutorial series, we’ll look at the compatibility issues when using objects that were constructed before C++20 with the three way comparison operator and how to resolve them

1. Introduction

Consider the code below:

struct Cpp17{
int num_;
bool operator==(const Cpp17& rhs)const{
return (num_ == rhs.num_)
}

bool operator <(const Cpp17& rhs)const{
return (num_ < rhs.num_);
}
bool operator >(const Cpp17& rhs)const{
return (num_ > rhs.num_);
}
}
struct Cpp20{
Cpp17 obj_…

image © : gajendra gulgulia

In the fifth and sixth part of the tutorial series, I explained the comparison category std::strong_ordering and std::weak_ordering respectively with examples and use cases. In this part of the tutorial series, we take a closer look at the third and final comparison category, i.e. std::partial_ordering.

std::partial_ordering has already been used in part four of the tutorial series to demonstrate how can it be used to indicate that two objects might not be comparable in which case we return std::partial_ordering::unordered and consequently all the binary relational operators except for operator!= returns false . …

image ©: gajendra gulgulia

In the fifth part of the tutorial series, I demonstrated the usage of std::strong_ordering comparison category as a return type for the three way operator. In the process, I clarified the meanings of terminologies like value of an object, salient properties and substitutablity. In the current part I’ll explain the usage of second comparison category, i.e., std::weak_ordering defined in the C++20’s new compare header.

Weak ordering allows to objects to be allowed for equivalence instead of an exact equality. In the fourth tutorial, I only discussed about equivalence and did not define objectively.

std::strong_ordering has four valid values: (1) less

image ©: gajendra gulgulia

In the fourth part of the tutorial, I introduced the theoretical ideas behind the return types of the three way operator in C++20 and demonstrated that there might be semantic restrictions on the program that allow for comparison of equivalence instead of equality. Also I demonstrated by a simple example when can two objects be semantically incomparable, even though the syntax of program allows to compare them and how to deal with such cases with the help of operator<=>.

In this part of the tutorial, I’ll explain one of the three comparison category in the compare header which is the…

image ©: gajendra gulgulia

In the third part of the tutorial series, I uncovered the mechanics of operator <=> and explained in detail, how the compiler re-writes the comparison expression on a custom object with only operator<=> declared as default and how additionally it can make use of synthesized expression to reverse the operands during expression re-writing process. If you haven’t read the third part, I strongly encourage you to read it before reading this tutorial.

As promised at the end of the third part, in this part I’ll explain the theory behind the concrete return types of the C++20’s three way operator. Honestly…

image ©: gajendra gulgulia

In the second part of the tutorial series, I touched upon the rules of the default operator<=> . Unlike default constructors or destructors, the default version of the <=> is not available automatically but has to be declared in the interface of the class and then only it is available to be used.

In this part of the tutorial series, I’ll explain the mechanics of the default operator<=> to further elucidate the rules of the three-way operator, i.e. …

image ©: gajendra gulgulia

In the first part of the tutorial series, I laid out the motivation behind C++20’s three way comparison operator: <=> and showcased the simplest use case how it can be useful to remove the boilerplate code for comparison operators : ==, >=, <=, !=, >, < if the semantics of the program requires us to implement them and how they can be gotten rid of by using the defaulted version of three way comparison operator

class Int{
...
friend auto operator<=>(const Int& lhs, const Int& rhs)=default;
};

In the this part of the tutorial series, I’ll explain what it means…

Gajendra Gulgulia

I'm a backend software developer and from time to time I also like to explore web development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store