📜 ⬆️ ⬇️

As I wrote a sentence to the standard C ++

This will be the story of a junior developer from Yandex.Passport about the appearance of a proposal in the C ++ standard, developed in collaboration with Anton antoshkka Polukhin. As often happens in life, something new began with pain, or rather, with the desire to stop it.


Once upon a time there was a library in my support. Everything about her was good: she was going under Linux, she worked, she didn't fall. One day, people came with a request (requirement) to assemble it under Windows. Why not? But the first time did not work. The root of evil was handwritten cryptography, which at some point multiplied two 64-bit integers. To save the result of such a multiplication, a number by 128 bits is required, and the library used the type __int128. It is beautiful: it has a natural interface, is supported by several compilers (gcc, clang), it works without memory allocation, but the main thing is that it exists.

The developers of the compiler from Microsoft did not provide support for this type, they did not come up with analogues - or I did not find them. The only cross-platform solution that came to mind is Big Numbers from OpenSSL, but it is somewhat different. As a result, I specifically solved this problem with a “bicycle”: I needed only uint128_t with a limited set of operations. From several alien solutions I collected the UInt128 class, put it in the library source. "Bicycle" - just the very pain. The task has been solved.
')
In the evening of the same day, I went to unwind at the event, where people from “Working Group 21” (WG21) talked about how they handle the C ++ file. I listened and wrote a short letter to cpp-proposal@yandex-team.ru from two sentences on the topic “I need int128 to cpp”. Anton Polukhin responded that the developers of the standard want to solve this problem once and for all. It is logical: now I needed a number for 128 bits, and someone needs to work with numbers for 512 bits - and this someone also wants a handy tool.

Anton also said that there are two ways to solve: through the core of the language and through the library. There is an opinion, and I share it, that the syntax of the language is already quite complicated: it will be very difficult to add a construction to the language that will provide a cross-platform and effective ability to use numbers of different accuracy. But within the library it is quite realistic to cope: the templates are our everything. “We need a working prototype,” said Anton. “And preferably with tests.” It also turned out that the type should be plain old data (POD) in order to appeal to more people.

And I went to make a prototype. The name wide_int was chosen deliberately: there are no stable associations with such a name, in any case, widespread ones. For example, big_number could be misleading - they say, it stores the value on the heap and never overflows. I wanted to get a type with behavior similar to the behavior of fundamental types. I wanted to make a type whose size will continue their progression: 8, 16, 32, 64 ... 128, 256, 512, etc. After a while, a working prototype appeared. Making it turned out to be easy: it had to compile and work, but not necessarily really efficiently and quickly.

Anton studied it, made a number of comments. For example, there was not enough conversion to floating point numbers, it was necessary to mark the maximum number of methods as constexpr and noexcept. From the idea of ​​restricting the choice of the size of the number of Anton, I was dissuaded: he made a size multiple of 64. After that, together with Anton, we wrote the text of the sentence itself. It turned out that writing a document is much more difficult than writing code. A little more polishing - and Anton (as the only one who understands what to do next) began to show our proposal to people from the standardization commission.

Criticized a little. For example, someone expressed a desire to make an integer type that does not overflow. Or a type whose size can be set with bit accuracy (and get, for example, a size of 719 bits!). The proposal to abandon the binding to the number of bits, and to set the number of machine words, seemed to me to be the strangest: business logic doesn’t care how many words are in a number on any platform - it is important for it to uniquely identify the same numbers on different platforms. For example, a unique user ID is an unsigned integer of 64 bits, and not one unsigned long long.

In a nutshell, these were ideas for other types. We listened to criticism, there were no significant comments. After that, Anton published a proposal to the standard and went to defend it at the next meeting of the commission .

The defense was successful: our proposal was taken to work - in other words, it will be considered at meetings and beyond. A number of comments were made; Now we make the necessary corrections. In particular, the commission still asked to operate with the number of machine words in wide_int. The argument is simple: the type will be implemented one way or another, but if you use these same machine words, it will come out more efficiently. I still have hope that the convenient uint128_t alias will fall into the standard - then I will be able to throw out my UInt128 type until someone else sees it. =)

The latest version of the implementation can be found here . There is also a document and a small discussion on stdcpp.ru . Just from the day of sending the first letter to cpp-proposal@yandex-team.ru about four months have passed. Of these, about 40 hours of non-working time I spent on this offer. At the time of this writing, the implementation is swollen by 1622 lines, and even the tests have added 1940 lines.

I consider it important to tell me what all this gives me. First, it is interesting to learn how the decision-making process is built and what is important when designing interfaces in the standard library.

Secondly, I can change C ++ in the direction I like. Of course, it is important to remember here that like-minded people are needed to implement any major idea. For example, I have an idea to make the interface for containers and strings a little more expressive and obvious: I wanted to add operator bool () to containers. But not indifferent to C ++ colleagues made it clear that I was wrong .

Thirdly, I learned a lot about new templates in C ++.

Fourthly, they say that this will somehow strengthen my resume ... I have not checked it yet, but I will take my word to experienced colleagues.

Fifthly, when Bjarne Straustrup somewhere in the correspondence devoted to the discussion of your work, writes to someone “+1” - this is fun. =) Even if he supports someone's criticism.

Finally, I’ll say that you can find out about the news and events of WG21 C ++ by subscribing to the stdcppru channel on Twitter.

Source: https://habr.com/ru/post/327080/


All Articles