C++17 In Detail

18 February 2019

Five Awesome C++ Papers For Kona 2019 ISO Meeting

Kona 2019 ISO C++ Meeting

I’m just in time (I hope)! In a few hours, a new C++ISO meeting will start! This time the committee gathered in Kona, Hawaii for their first meeting in 2019.

Let’s see what’s already in C++20 and let’s have a look at some smaller, but very handy proposals that might get into the standard.

What’s Already in C++20

Thanks to various trip reports and especially r/cpp threads we can gather a list of features that are already merged into C++20. It’s not complete, but I tried to list the most essential elements.

At cppreference there's also a single list with all C++20 features: C++2a features

Toronto, July 2017

The first meeting that discussed and merged things for C++20

  • Concepts - P0734r0
  • Explicit generic lambdas - P0428r2
  • _VA_OPT_ - P0306r4
  • Default bit-field initializers - P0710r1
  • Fixed const-qualified pointers to members P0704r1
  • Allow [=, this] as a lambda capture - p0409r2
  • Designated Initializers - p0329r4
  • More deduction guides for the standard library - p0702r1
  • Endian - p0463r1
  • Arrays for make_shared - p0674r1

Gathered from r/cpp - 2017 Toronto ISO C++ Committee Discussion Thread (Concepts in C++20; Coroutines, Ranges and Networking TSes published)
(Thanks Yehezkel for reminding about that meeting!)

Added in Albuquerque, November 2017:

  • operator<=> (aka the spaceship operator) and library support for operator<=> P0515
  • Range-based for with initializer P0614
  • Apply [[nodiscard]] to the standard library - P0600
  • std::osyncstream P0053
  • constexpr std::complex
  • constexpr algorithms P0202
  • Floating point std::atomics P0020
  • std::string/std::string_view .starts_with() and .ends_with() P0457

Gathered from 2017 Albuquerque ISO C++ Committee Reddit Trip Report : r/cpp

Another C++20 meeting, Jacksonville, March, 2018:

  • Make typename optional in more places
  • [[likely]] , [[unlikely]] and [[no_unique_address]] - attributes
  • <version> header
  • Calender and timezone library - big and nice addition to STL - P0355
  • syncstream manipulators for C++ Synchronized Buffered Ostream
  • span P0122
  • [Pack expansion in lambda init-capture: ...args = std::move(args)](){} P0780

More info: 2018 Jacksonville ISO C++ Committee Reddit Trip Report : cpp

June 2018, Rapperswil:

  • Contracts P0380
  • Standard library concepts P0898
  • Class non type template parameters (NTTP)P0732
  • Feature test macros P0941
  • Conditional explicit P0892
  • Virtual calls in constant expressions P1064
  • atomic_ref P0019
  • shift_left and shift_right algorithms P0769
  • type_identity P0887
  • ispow2, ceil2, floor2, log2p1 P0556
  • bit_cast P0476
  • Remove facilities deprecated in C++17 P0619

Gathered from r/cpp: 2018 Rapperswil ISO C++ Committee Trip Report

Added in San Diego, November 2018:

  • Ranges! - P0896
  • void foo(Concept auto x)- style terse syntax for concepts - P1141.
  • consteval functions - P1073
  • std::is_constant_evaluated - P0595
  • constexpr elements:
    • union - P1330
    • try and catch - P1002
    • dynamic_cast and typeid - P1327
    • std::pointer_traits.
    • Miscellaneous constexpr library bits.
  • Signed integers are two’s complement - P1236
  • char8_t - P0482
  • Nested inline namespaces - P1094
  • Heterogeneous lookup for unordered containers P0919

Additionally, during this meeting, the Committee agreed on a single plan for modules, so there’s a higher chance we’ll see them in C++20.

Gathered from r/cpp: 2018 San Diego ISO C++ Committee Trip Report and Trip Report: C++ Standards Meeting in San Diego, November 2018 | There’s Waldo!

Upcoming Meetings

You can always find the list of ISO meetings here:
Upcoming Meetings, Past Meetings: Standard C++

The next meeting will take place in Europe! This time in Cologne, Germany.

Awesome Papers

The papers (mailings) for the upcoming San Diego meeting can be found under the following link:
JTC1/SC22/WG21 - Papers 2019

Ok, so below you can find my list of papers that brought my attention:

P1301 [[nodiscard("should have a reason")]]

P1301

Excellent update for my favourite C++ attribute!

With C++17 you can only mark some code without any explanation.

For example:

[[nodiscard]] 
T* allocate_memory() {  T* ptr = system_allocate(...); return ptr; }

With the proposal you’ll be also able to set a meaningful message:

[[nodiscard("possible memory leak!")]]
T* allocate_memory() {  T* ptr = system_allocate(...); return ptr; }

It’s a similar story as with [[deprecated]] attribute that also has an option to take a message: [[deprecated( string-literal )]].

P0829 - Free Standing Mode

P0829R4

In short, as I understand correctly, the paper discusses an option to divide the standard Library into two “modes”: hosted and freestanding.

The hosted version requires some OS/hardware interaction. For example std::filesystem is a good example of that - in some lower layer, it needs to call OS file manipulation functions. Similarly std::thread or other threading objects.

The freestanding version relies only on the language and the compiler. It might be especially useful in low-level environments like GPU, kernel, embedded. In those areas a programmer might want to use std::tuple, but he doesn’t want to pay for the ability to work with std::filesystem.

Some proposed elements of the freestanding mode: <ranges>, <tuple>, <utility>, and portions of other headers.

This paper might also be linked to the opportunity that we’ll soon have: split the library into separate modules (when modules get accepted). Maybe it’s a reorganisation opportunity, and we could clean some things now? (or maybe for C++23)?

P0798 - Monadic operations for std::optional

P0798

This proposal adds a few methods to std::optional and allows us to write simpler code with multiple “chained” optional values.

new methods:

  • transform - transforms a value or returns empty optional
  • and_then - is used to compose functions which return std::optional
  • or_else - returns the optional if it has a value, otherwise it calls a given function

you can read more in a blog post from Simon Brand (the author of this proposal): Functional exceptionless error-handling with optional and expected.

P1361R0 - Integration of chrono with text formatting

P1361R0

This is a collaboration effort to make formatting functions better for C++20!

Calendar and chrono extensions for C++20 (already merged) contains own functionalities for text formatting. At the same time {fmt} library is also targetted for C++20… so why not combine those two and create a single solution?

This paper proposes common functionalities that will handle chrono and plug them into {fmt} routines.

for example (using the paper):

void print_birthday(std::string_view name,
                    const std::chrono::year_month_day& birthday)
{
    std::cout << std::format("{0}'s birthday is {1:%Y-%m-%d}.\n",
                             name, birthday);
}

The function std::chrono::format (that is even already listed at cppreference: std::chrono::format) will be replaced by more general formatting routines.

P0920 - Precalculated hash values in lookup

P0920, proposed by Mateusz Pusz

In business scenarios it often happens that we have to look for the same keyword in more than one container at a time. Doing that is expensive right now as it forces hash value recalculation on every lookup.

The paper adds function overloads for unordered_map, unordered_multimap, unordered_set and unordered_multiset that allows you to pass a pre-calculated hash value for the element.

// code from P0920:
const auto hash = maps.front().hash_function()(user); // calc one
  for(auto& m : maps) {
    auto it = m.find(user, hash); // use in several maps
    // ...
  }

The paper also mentions that early benchmarks showed between 20% and 50% performance improvements. The new functionality might be a significant improvement for advanced unordered containers usage.

Summary

This article contained just a quick run of some interesting C++ papers. What’s your opinion about them?

What’s your favourite proposal?

What things would you like to see in C++20?

C++17 In Detail
© 2017, Bartlomiej Filipek, Blogger platform
Disclaimer: Any opinions expressed herein are in no way representative of those of my employers. All data and information provided on this site is for informational purposes only. I try to write complete and accurate articles, but the web-site will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.