C++20: The Library

Contents[Show]

My last post "C++20: The Core Language" presented the new features of the C++20 core language. Today, I continue my journey with an overview of the C++20 library.

TimelineCpp20LibrariesThe image shows you my plan for today.

Library

Calendar and Time-Zone

The chrono library from C++11/14 was extended with calendar and time-zone facility.  If you don't know the chrono library, read my posts to time.

Calendar

Calendar: consists of types, which represent a year, a month, a day of a weekday, and an n-th weekday of a month. This elementary types can be combined to complex types such for example year_month, year_month_day, year_month_day_last, years_month_weekday, and year_month_weekday_last. The operator "/" is overloaded for the convenient specification of time points. Additionally, we will get with C++20 new literals: d for a day and y for a year.

Time-Zone

Time-points can be displayed in various specific time-zones.

Due to the extended chrono library, the following use-cases are easy to implement:

  • representing dates in various forms
auto d1 = 2019y/oct/28;
auto d2 = 28d/oct/2019;
auto d3 = oct/28/2019; 

 

  • get the last day of a month
  • get the number of days between two dates
  • printing the current time in various time-zones

If you want to play with these features, use Howard Hinnards implementation on GitHub. Howard Hinnard, the author for the calendar and time-zone proposal, also created a playground for it on Wandbox.

#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    auto now = system_clock::now();
    std::cout << "The current time is " << now << " UTC\n";
    auto current_year = year_month_day{floor<days>(now)}.year();
    std::cout << "The current year is " << current_year << '\n';
    auto h = floor<hours>(now) - sys_days{jan/1/current_year};
    std::cout << "It has been " << h << " since New Years!\n";
}

Of course, C++20 uses the std::chrono namespace instead of the date namespace. Here is the output of the program:

calendar

 

std::span

A std::span stands for an object than can refer to a contiguous sequence of objects. A std::span, sometimes also called a view is never an owner. This contiguous memory can be an array, a pointer with a size, or a std::vector. A typical implementation needs a pointer to its first element and a size. The main reason for having a std::span<T> is that a plain array will be decay to a pointer if passed to a function; therefore, the size is lost. std::span<T> automatically deduces the size of the plain array or the std::vector. If you use a pointer to initialise a std::span<T>, you have to provide the size for the constructor.

template <typename T>
void copy_n(const T* p, T* q, int n){}

template <typename T>
void copy(std::span<const T> src, std::span<T> des){}

int main(){
    
  int arr1[] = {1, 2, 3};
  int arr2[] = {3, 4, 5};
  
  copy_n(arr1, arr2, 3);         // (1)
  copy(arr1, arr2);              // (2)
    
}

 

In contrast to the function copy_n (1), copy (2) don't need the number of elements. Hence, a common cause of errors is gone with std::span<T>.

constexpr Containers

C++ becomes more and more constexpr. For example, many algorithms of the Standard Template Library get with C++20 a constexpr overload.  constexpr for a function or function template means that it could potentially be performed at compile time. The question is now, which containers can be used at compile time? With C++20, the answer is std::string and std::vector.

Before C++20, both can not be used in a constexpr evaluation, because there were three limiting aspects.

  1. Destructors couldn't be constexpr.
  2. Dynamic memory allocation/deallocation wasn't available.
  3. In-place construction using placement-new wasn't available.

These limiting aspects are now solved.

Point 3 talks about placement-new, which is quite unknown. Placement-new is often used to instantiate an object in a pre-reserved memory area. Besides, you can overload placement-new globally or for your data types.

char* memory = new char[sizeof(Account)];        // allocate memory
Account* account = new(memory) Account;          // construct in-place
account->~Account();                             // destruct
delete [] memory;                                // free memory

 

Here are the steps to use placement-new. The first line allocates memory for an Account, which is used in the second line to construct an account in-place. Admittedly, the expression account->~Account() looks strange. This expression is one of these rare cases, in which you have to call the destructor explicitly. Finally, the last line frees the memory.

I will not go further into the details to constexpr Containers. If you are curious, read the proposal 784R1.

std::format

cppreference.com/ has a concise description of the new formatting library: "The text formatting library offers a safe and extensible alternative to the printf family of functions. It is intended to complement the existing C++ I/O streams library and reuse some of its infrastructure such as overloaded insertion operators for user-defined types.". This concise description includes a straightforward example:

std::string message = std::format("The answer is {}.", 42);

 

Maybe, this reminds you to Pythons format string. You are right. There is already an implementation of the std::format on GitHub available: fmt. Here are a few examples from the mentioned implementation. Instead of std, it uses the namespace fmt.

  • Format and use positional arguments
std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy");
// s == "I'd rather be happy than right."

 

  •  Convert an integer to a string in a safe way
fmt::memory_buffer buf;
format_to(buf, "{}", 42);    // replaces itoa(42, buffer, 10)
format_to(buf, "{:x}", 42);  // replaces itoa(42, buffer, 16)
// access the string with to_string(buf) or buf.data()

 

  • Format user-defined types
struct date {
  int year, month, day;
};

template <>
struct fmt::formatter<date> {
  template <typename ParseContext>
  constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }

  template <typename FormatContext>
  auto format(const date &d, FormatContext &ctx) {
    return format_to(ctx.out(), "{}-{}-{}", d.year, d.month, d.day);
  }
};

std::string s = fmt::format("The date is {}", date{2012, 12, 9});
// s == "The date is 2012-12-9"

 

What's next?

 

As promised, I will dive deeper with a future post into the library. But first, I have to finish my high-level overview of C++20. My next post is about the concurrency features.

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

Tags: C++20

Comments   

0 #1 Peter Lauro 2020-06-06 14:02
Concerning "constexpr" function definition, it would be worth to mention a new std trait

https://en.cppreference.com/w/cpp/types/is_constant_evaluated

which helps the function to detect the context in which the function is invoked.
Quote
0 #2 Peter Lauro 2020-06-06 16:24
concerning the std::span example:
in the case of template "copy" function (with the current signature), the compilers are not capable to find the function for int[3] types. the implicit conversion from int[3] to spawn is not applied.

the call of copy method needs to employ the explicit conversion from array to span.

copy(std::span(arr1), std::span(arr2));
Quote
0 #3 Rainer Grimm 2020-06-11 08:18
I will write a post about std::is_contant_evaluated in the future. This was only an overview.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 3571

Yesterday 7709

Week 27667

Month 186098

All 5055412

Currently are 183 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments