C++ Club - Meeting 146

Episode Date: June 17, 2022

https://cppclub.uk/meetings/2022/146/...

Transcript
Discussion (0)
Starting point is 00:00:00 Welcome to C++ Club. This is meeting 146 that took place on the 31st of March 2022. March committee mailing is available. It contains some existing papers with new revisions and some new papers. Let's have a look at some of them. T0009, MD-SPAN Revision 16 of this paper contains major wording revisions based on Luji feedback. To remind you, this paper proposes adding to the C++ standard library a multidimensional array view along with classes, class templates and constants for describing and creating multidimensional array views. It also proposes adding the subMDSpan
Starting point is 00:00:57 function that slices returns an MDSpan that views a subset of an existing MDSpan." End quote. At this point why not make a bet on which revision of this paper gets into C++ 26? I say 21. P1684. StudMDArray. A companion to the previous paper, this one proposes an owning equivalent of MDSpan. P2558, add ampersand, the dollar sign and a backtick to the basic character set. Steve Downey wrote this paper in which he says, quote,
Starting point is 00:01:49 WG14, the C standardization committee, is adopting N2701 for C23. This will add these symbols to the basic source character set. C++ should adopt the same characters for C++26. End quote. Previously, Karentin Jabot discussed the usage of these characters in other programming languages in his paper named For a Few Punctuators More. P2565, supporting user-defined attributes.
Starting point is 00:02:29 Brett Brown of Bloomberg proposes to add support for user-defined attributes. At the moment, compilers warn you if they encounter an unknown attribute. And at the same time, they are allowed to define compiler-specific attributes, like clang-nosanitize-undefined, for example. Because of this cross-platform, libraries often have to resort to dances with macros to support code that depends on a vendor-specific attribute. The author proposes to keep the unknown attribute warning, but allow a new syntax for user-defined attributes.
Starting point is 00:03:09 It would use an attribute modifier extern. So it would look like extern, GNU access, and then some attribute. This is an example of a proposed external custom attribute. The author chose the extern keyword for this because I guess this keyword has too few meanings in C++ and could use one more. Since attributes are built into compilers, it's not clear to me how one would define a custom attribute. Next, we have a post by Bryce Lelbach titled It's Time for New C++ Leadership. Bryce writes, quote, C++ is at an inflection point.
Starting point is 00:04:07 The C++ committee has been successful over the past decade. Yet the landscape has changed, and we must adapt to be successful in the future. We need engaged new leadership to address the challenges we face and to build the next generation of C++ leadership. I, Bryce Adelstein Lelbach, am seeking the US nomination for the position of ISO IEC JTC1 SC22 WG21 convener
Starting point is 00:04:36 to provide such leadership. End quote. Bryce named several problems with how C++ is being developed. Quote, The C++ committee and the number of proposals we receive has grown tenfold over the last decade.
Starting point is 00:04:56 For the last six years, we have had a significant backlog of papers waiting for review. In some cases, papers wait years to be reviewed by a subgroup. As Bjarne wrote in PO 977, quote, Most people on the C++ Committee are working independently towards non-shared goals. We are 300 individual authors, not one team. We lack focus and a shared set of priorities. End quote. not one team, will lack focus and a shared set of priorities.
Starting point is 00:05:29 Bryce proposes steps to alleviate the situation. Regarding the new ways of working that emerged during the pandemic, he says, Our heavy reliance on face-to-face meetings seems outdated to the next generation of programmers who are used to modern collaboration tools that have become prevalent over the last two decades." He wants to assure that, quote, remote entities have an equal seat at all future face-to-face meetings. This is going to be one of the biggest challenges. Among other things, Bryce wants to unify the currently scattered infrastructure under the isocpp.org domain.
Starting point is 00:06:13 One of his most important promises is address the lack of diversity and inclusivity. Quote, the first step in fixing a problem is acknowledging it. The C++ community is not as diverse or inclusive as it should be. This threatens C++'s long-term legacy. As a convener, diversity, inclusion, safety, and growing the next generation of C++ committee leadership will be my top priorities."
Starting point is 00:06:44 Ambitious goals, and it'll be interesting to see how it goes for Bryce and the committee. XMake Package Management This article describes package management in CMake using VCBKG and Conan, and compares it to what's available in XMake. It also introduces XMake's standalone package manager XRepo. I'm still amazed at the quality and capabilities of XMake. We lament about how difficult it is to bootstrap a C++ project. We have entire tools that bootstrap CMake projects. But here it is, an easy to use and amazingly capable build system, and nobody seems to know about it. CMake is the de facto standard, but teaching it to students
Starting point is 00:07:37 is akin to starting a modern C++ course by explaining pointers. XMake could be an ideal student-friendly introduction to build systems at least for their toy projects, to avoid scaring them away before they even start learning C++. NFT pointer I only later learned that this was an April Fool's 2021 joke, but still it's good. Non-fungible tokens, or NFTs, are a scam built on the blockchain technology. There are many articles explaining this latest high-tech planet-destroying pyramid scheme,
Starting point is 00:08:28 so I'm not going to go into that. Instead, let me tell you about this excellent project that highlights the craziness from the C++ pointer view. Behold, NFT pointer. Quote, C++ stood unique pointer that represents each object as an NFT on the Ethereum blockchain. For an April Fool's joke, this is quite a sophisticated one and I think it works, sort of. This is a code snippet. sophisticated one and I think it works, sort of. This is a code snippet. moving between two NFT pointers. First you make NFT of the type cow, for example. Then
Starting point is 00:09:21 you create another NFT pointer of the type Animal, and then you assign the cow NFT pointer, or rather move it into the Animal pointer. Quote, this transfers the non-fungible token, the token ID, representing the cow's memory address from pointer 1 on opencetherscan to pointer 2 opencetherscan. End quote. So yeah, it works and is completely bonkers. I especially like the Y section. C++ memory management is hard to understand, opaque and not secure.
Starting point is 00:10:06 As we all know, adding blockchain to a problem automatically makes it simple, transparent and cryptographically secure. Thus, we extend Studunique Pointer, the most popular C++ smart pointer used for memory management, with blockchain support. Written in Rust for the hipster cred. Made with love by a blockchain expert who wrote like 100 lines of Solidity in 2017, which didn't work.
Starting point is 00:10:38 The performance section doesn't disappoint either. Quote, NFT pointer has negligible performance overhead compared to StudUnique pointer, as shown by this benchmark on our example program. StudUnique pointer, 0.005 seconds. NFT pointer, 3 minutes. The project is very thorough and even has a link to a white paper, which is indeed a sheet of white paper in a PDF format. Next we have comparing floating-point numbers is tricky. This is an old article from 2017, but it's still useful and provides a good illustration
Starting point is 00:11:32 of the problems with machine representation of floating-point numbers. Good things to remember. First of all, floats cannot store arbitrary real numbers, or even arbitrary rational numbers. And since the equations are exponential, the distance on the number line between adjacent values increases exponentially as you move away from zero. Over the course of this article, the author develops and improves a function to compare two floating-point numbers. He starts with this code, which I've seen many times in our codebases, and explains
Starting point is 00:12:16 why it's wrong. This is taking a difference between two floating numbers, taking its absolute value, and checking that it's less than or equal to float epsilon value. Quote, We would hope that we are done here, but we would be wrong. A look at the language standard reveals that flt epsilon is equal to the difference between 1 and the value that follows it.
Starting point is 00:12:48 But as we noted before, float values aren't equidistant. For values smaller than 1, flt epsilon quickly becomes too large to be useful. For values greater than 2, flt epsilon is smaller than the distance between adjacent values. So the condition will always be false." End quote. What about Boost? Boost has floating-point comparison API, but the author explains how it is also not quite correct.
Starting point is 00:13:23 He then arrives at ULPS. What about ALPS? It would be nice to define comparisons in terms of something more concrete than arbitrary thresholds. Ideally, we would like to know the number of possible floating-point values, sometimes called Units of Least Pre least precision or ULPs. ALPs? I'll go with ULPs. Between inputs. If I have some value A and another value B is only two or three ULPs away, we can probably consider them equal, assuming some rounding error.
Starting point is 00:14:06 Most importantly, this is true regardless of the distance between A and B on the number line." The author emphasizes the fact that alps don't work for comparing values close to zero, but that can be handled as a special case. The main takeaways from this article are When comparing floating-point values, remember, FLT epsilon isn't float epsilon except in the ranges of and. The distance between adjacent values depends on the values in question. When comparing to some known value, especially zero or values near it, use a fixed epsilon
Starting point is 00:14:54 value that makes sense for your calculations. When comparing non-zero values, some OLPS-based comparison is probably the best choice. When values could be anywhere on the number line, some hybrid of the two is needed. Choose epsilons carefully based on expected outputs. This article was adapted from Bruce Davison's article comparing floating point numbers 2012 edition. The Google test macro AssertNear uses a combination of ULPS and Epsilon-based comparisons and is the best way to compare floating pointpoint values in tests against an epsilon difference. David Goldberg's article, What Every Computer Scientist Should Know About Floating-Point Arithmetic, is a required reading for all programmers.
Starting point is 00:15:59 A web-based version is on your screen right now. A related article by John D. Cook called floating-point numbers are a leaky abstraction points out a few cases when floating-point numbers don't behave as expected. Quote floating- numbers, the computer representation of real numbers, are leaky abstractions. They work remarkably well. You can usually pretend that a floating-point type is a mathematical real number. But sometimes you can't. The abstraction leaks, though not very often. End quote.
Starting point is 00:16:50 Herbie. Herbie is a neat tool that simplifies arithmetic expressions to avoid floating point issues. As you can see, it optimizes an equation to minimize errors. Quote, Herbie detects inaccurate expressions and finds more accurate replacements. The left expression, which is square root of x plus 1 minus square root of x, is inaccurate when x is greater than 1. Herbie's replacement is accurate for all x's. End quote.
Starting point is 00:17:33 Herbie can be installed locally or used from the web demo page. It is programmed in Racket. Never heard that before. It looks like a Lisp-like language for language processing. Binary number representation cheat sheet. Hacking CPP provides a useful cheat sheet for floating-point number representations. It covers signed integers, unsigned integers, and floating-point numbers of different sizes. Very useful. Qt Creator 7 has been released. Even if you don't use Qt, this is a great general-purpose C++ IDE,
Starting point is 00:18:27 which is now based on ClangD, the Clang demon, using Language Server Protocol. Debugging with GDB. The IncrediBuild blog has another interesting and useful article called A Step-by-Step Crash Course in C++ Debugging Without IDE Empowering the Terminal by Using GDB written by Adam Sigouli Schubert. It covers the basics of GDB command-line interface and its text user interface mode. The Reddit thread participants seem to have trouble understanding why in the era of ubiquitous C++ IDEs would anyone want to use a terminal-based debugger.
Starting point is 00:19:19 Lucky people, they must never have had to inspect a crash dump over an SSH connection to a Linux server. Unlib, a lightweight header-only dependency-free C++14 library for ISO units. The author SBI from Berlin wrote yet another physical units library that supports SI ISO units and requires C++14. It's header-only and comes under Boost license. Quote, A minimal header-only C++-compatible SI unit library, providing quantities that behave like arithmetic types
Starting point is 00:20:02 and feature physical dimensions like power, scaling like kilo, and tagging of units. If your code has to deal with physical units, you can use this library so that the compiler checks your usage of dimensions and your formulas at compile time." The library features user-defined literals, so you can say 1.__kilogram, 9.81__M__P__S2, or 1 underscore h for hour. It allows deriving units from the basic set. So you can have an electrical charge unit that uses multiplication of units of current and time.
Starting point is 00:21:05 There is also support for ratios and creation of scaled quantities from basic ones. For example, using milligram as unlibmili of unlibgram, or using kilogram as unlib-kilo of unlib-gram, or using ton as unlib-kilo of kilogram. The author says in the comment, humans don't use megagram. The library supports tags for when different quantities, which must not be confused, are represented by the same physical unit. Unit conversions are supported by various casts.
Starting point is 00:21:53 Quote. There are four different kind of casts available. Value cast allows casting between units with different value types. For example, seconds in int versus seconds in long long. Scale cast allows casting between units with different scales, like seconds and minutes. Tag cast allows casting between units with different tags, like active and reactive power. And quantity cast allows casting between units where value types, scales and tags might be different." So it looks like a very useful dimensional analysis library. There is similar work available, like for example the Units library by Mateusz Pusch, which is a subject of standardization effort. This one requires C++20, uses concepts, and comes under MIT license.
Starting point is 00:22:52 This library uses literals, but also allows you to specify units as multipliers. Boost units by Mateusz Szabel and Stephen Watanabe implements dimensional analysis in a general and extensible manner, treating it as a generic compile-time metaprogramming problem. It uses Boost MPL and is slow to compile, but allows you to define your own unit systems. And Units by Nick Holdhouse is a compile-time header-only dimensional analysis and unit conversion library built on C++14 with no dependencies. It comes under RAMID license. Each unit has its own type. Literals are supported.
Starting point is 00:23:41 And unit conversions and manipulations are very fast and efficient. Next we have a tweet by Martin Hozhenovsky. He tweeted, As we all know, std vector of bool keeps causing endless issues. But did you know you can use std basic string of bool instead, avoiding the terrible proxy objects? You even get a small vector optimization for free. Apparently there is also a stack overflow question about that.
Starting point is 00:24:16 It seems like the idea might work, but you still shouldn't do it. Some of the tweet replies were Jan Wilmans. Can somebody get people like this away from the keyboard before they hurt themselves? And Michael Case. Somehow I equally love and hate this tweet. That's it for today and I'll leave you with some amusing stuff i found this one's on twitter molly's truth tweets the plural of regex is regrets yep next we have a photo of an exam paper with the answer crossed as incorrect.
Starting point is 00:25:07 The question reads, Name and describe the five key phases of software development. And apparently, incorrect answer was Denial, bargaining, anger, depression, and acceptance. Yeah, I can feel that. And finally, a quote from Twitter by Kyriakos Kapakulak. I learn from the mistakes of people who take my advice. That's it.
Starting point is 00:25:41 Thanks for joining me. Until next time. Bye bye.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.