{"id":390778,"date":"2024-06-29T09:26:43","date_gmt":"2024-06-29T09:26:43","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=390778"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=390778","title":{"rendered":"<span>C2x: the future C standard<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/7j\/dw\/5r\/7jdw5r9gajn2olyfuypvvwskuve.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/webt\/7j\/dw\/5r\/7jdw5r9gajn2olyfuypvvwskuve.png\"\/><\/p>\n<p>  <\/p>\n<blockquote><p>I strain to make the far-off echo yield<br \/>  A cue to the events that may come in my day.<br \/>  (\u2018Doctor Zhivago\u2019, Boris Pasternak)<\/p><\/blockquote>\n<p>I\u2019ll be honest: I don\u2019t write in pure C that often anymore and I haven\u2019t been following the language\u2019s development for a long time. However, two unexpected things happened recently: \u0421 <a href=\"https:\/\/www.tiobe.com\/tiobe-index\/\">won back<\/a> the title of the most popular programming language according to TIOBE, and the first truly <a href=\"https:\/\/nostarch.com\/Effective_C\">interesting<\/a> book in years on this language was published. So, I decided to spend a few evenings studying material on C2x, the future version of C.<\/p>\n<p>  <\/p>\n<p>Here I will share with you what I consider to be its most interesting new features.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"the-committee-the-standard-and-all-that\">The Committee, the Standard and all that<\/h1>\n<p>  <\/p>\n<p>I am sure most of you know how C is developed, but, for anyone who doesn\u2019t know let me first explain the terminology, and will then briefly retell the story of the language.<\/p>\n<p>  <\/p>\n<p>In 1989 the already extremely popular programming language, C, reached new heights of recognition, becoming both the American national (<a href=\"https:\/\/en.wikipedia.org\/wiki\/American_National_Standards_Institute\">ANSI<\/a>) and the international (<a href=\"https:\/\/en.wikipedia.org\/wiki\/International_Organization_for_Standardization\">ISO<\/a>) standard. This version of C was known as C89, or ANSI C to differentiate it from the numerous semi-compatible dialects that had existed previously.<\/p>\n<p>  <\/p>\n<p>A new version of the language standard is released approximately once every ten years. At the present time there are four versions in existence: the original C89, C99, C11 and C18. It is not known when the next version will be published, so the version currently being worked on is referred to as C2x.<\/p>\n<p>  <\/p>\n<p>Changes to the standard are made by a special group, the so-called WG14. It comprises interested representatives of the industry from various countries.<\/p>\n<p>  <\/p>\n<p>In the specialist English-language literature this group is often referred to as the \u2018Committee\u2019, so that is what I am going to call it here as well.<\/p>\n<p>  <\/p>\n<p>The Committee receives proposals from those involved, each proposal being given a designation(e.g. <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2353.htm\">N2353<\/a>). Proposals usually include: the reason for introducing changes, references to other documents, and specific changes to the Standard. Proposals can come in several versions, each of which is given a unique designation.<\/p>\n<p>  <\/p>\n<p>Returning to our topic for a moment, I have split this article into three parts, and have ordered them according to the likelihood of the relevant changes being made to the standard. The three parts are as follows:<\/p>\n<p>  <\/p>\n<ol>\n<li>Proposals the Committee has already accepted.<\/li>\n<li>Proposals received positively but returned to the authors for revision.<\/li>\n<li>What I consider to be the \u2018juiciest\u2019 proposals: rumoured unpublished proposals, being discussed behind the scenes by members of the Committee.<\/li>\n<\/ol>\n<p>  <\/p>\n<h1 id=\"proposals-accepted-by-the-committee\">Proposals accepted by the Committee<\/h1>\n<p>  <\/p>\n<h3 id=\"strdup-and-strndup-functions\">strdup and strndup functions<\/h3>\n<p>  <\/p>\n<p>I may appear ignorant when I say that I wasn\u2019t aware these functions weren\u2019t in the standard C library. What could be more obvious and simpler than copying strings? But no, C isn\u2019t like that. C doesn\u2019t like its users.<\/p>\n<p>  <\/p>\n<p>So, 20 years later, we are <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2353.htm\">getting<\/a> the <code>strdup<\/code> and <code>strndup<\/code> functions!<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;string.h>  char *strdup (const char *s); char *strndup (const char *s, size_t size);<\/code><\/pre>\n<p>  <\/p>\n<p>It is nice to know that even the Committee accepts the inevitable.<\/p>\n<p>  <\/p>\n<h3 id=\"attributes\">Attributes<\/h3>\n<p>  <\/p>\n<p>Developers of major C compilers have a favourite game they play: coming up with extensions to the language most often expressed through attributes of declarations and definitions. The language itself, of course, does not provide any special syntax for such things, so each person needs to do what they can to be creative.<\/p>\n<p>  <\/p>\n<p>In order to \u2013 somehow \u2013 to sort out this mess without coming up with dozens of new keywords, the Committee thought up a syntax-to-rule-them-all. In a nutshell, a standard <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2335.pdf\">syntax<\/a> for specifying attributes will be approved as part of the next version. Here is an example from the proposal:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">[[attr1]] struct [[attr2]] S { } [[attr3]] s1 [[attr4]], s2 [[attr5]];<\/code><\/pre>\n<p>  <\/p>\n<p>Here, <code>attr1<\/code> relates to <code>s1<\/code> and <code>=s2=<\/code>; <code>attr2<\/code> relates to the <code>struct S<\/code> definition; <code>attr3<\/code> relates to the <code>struct s1<\/code> type; <code>attr4<\/code> to the <code>s1<\/code> identifier; and <code>attr5<\/code> to the <code>s2<\/code> identifier.<\/p>\n<p>  <\/p>\n<p>The Committee has already voted to include the attributes in the standard, but there is still a long time to wait before the updated version of the standard is published. Nevertheless, proposal authors are already playing with their new toy. Here are some of the proposed attributes:<\/p>\n<p>  <\/p>\n<ol>\n<li>The <code>deprecated<\/code> attribute allows you to mark a declaration as obsolete, which allows compilers to issue appropriate warnings.<\/li>\n<li>The <code>fallthrough<\/code> attribute can be used to explicitly mark the places in the switch case branches, where the control flow is supposed to cross case boundaries.<\/li>\n<li>Using the <code>nodiscard<\/code> attribute you can explicitly specify that a value returned by the function needs to be processed.<\/li>\n<li>Where a variable or function is not used deliberately, you can mark it with the <code>maybe_unused<\/code> attribute (instead of the idiomatic <code>(void) unused_var<\/code>).<\/li>\n<li>A function not returning to the call location can be marked with the <code>noreturn<\/code> attribute.<\/li>\n<\/ol>\n<p>  <\/p>\n<h3 id=\"old-school-function-parameter-declaration-style-kr\">Old-school function parameter declaration style (K&amp;R)<\/h3>\n<p>  <\/p>\n<p>\u2018K&amp;R declaration\u2019 (read \u201cwhen types are specified after the brackets\u201d or, \u201cI don\u2019t understand old code in C\u201d) is a form of function parameter declaration that was already out-of-date way back in 1989. It is finally going to be <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2432.pdf\">burnt with fire<\/a>. In other words, you won\u2019t be allowed to do this anymore:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">long maxl (a, b) long a, b; {     return a > b ? a: b; }<\/code><\/pre>\n<p>  <\/p>\n<p>Enlightenment has finally come to code in C! Function declarations will at last actually do what people expect them to:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">\/* function declaration without arguments *\/ int no_args();  \/* also function declaration without arguments *\/ int no_args(void);<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"signed-integer-representation\">Signed integer representation<\/h3>\n<p>  <\/p>\n<p>What has felt like an endless saga is nearing completion, it would seem. The Committee has <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2412.pdf\">come to terms<\/a> with the fact that there are no such things as unicorns or mythical architectures, and programmers in C are dealing with <a href=\"https:\/\/en.wikipedia.org\/wiki\/Two's_complement\">Two\u2019s complement<\/a> signed integer representation.<\/p>\n<p>  <\/p>\n<p>In its present form this clarification simplifies the standard a little, but in future it should make it possible to get rid of the language\u2019s favourite undefined behaviour.<\/p>\n<p>  <\/p>\n<h1 id=\"proposals-being-worked-on\">Proposals being worked on<\/h1>\n<p>  <\/p>\n<p>While it can be said that the changes listed above already exist in our reality, the following group of proposals is still being developed. Nevertheless, the Committee has given them provisional approval and, assuming the authors show due diligence, they should definitely be accepted.<\/p>\n<p>  <\/p>\n<h3 id=\"anonymous-function-parameters\">Anonymous function parameters<\/h3>\n<p>  <\/p>\n<p>I regularly write 1-2 trial programs in C a week. And, quite honestly, I have long grown tired of having to specify the names of unused arguments.<\/p>\n<p>  <\/p>\n<p>Implementing <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2510.pdf\">one<\/a> of the proposals positively assessed by the Committee would mean that we wouldn\u2019t have to keep specifying the names of parameters in function definitions:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">int main(int, char *[]) {     \/* No hassle! *\/     return 0; }<\/code><\/pre>\n<p>  <\/p>\n<p>It\u2019s a small thing \u2013 but welcome!<\/p>\n<p>  <\/p>\n<h3 id=\"the-old-new-keywords\">The old new keywords<\/h3>\n<p>  <\/p>\n<p>After a very loooong transition period the Committee, finally, decided to accept, erm, <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2458.pdf\">\u2018new\u2019<\/a> <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2457.pdf\">keywords<\/a> into the language: <code>true<\/code>, <code>false<\/code>, <code>alignas<\/code>, <code>alignof<\/code>, <code>bool<\/code>, <code>static_assert<\/code> and others. It will finally be possible to drop headers like <code>&lt;stdbool.h><\/code>.<\/p>\n<p>  <\/p>\n<h3 id=\"including-binary-files-in-the-source-file\">Including binary files in the source file<\/h3>\n<p>  <\/p>\n<p>The <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2499.pdf\">option<\/a> of including binary data from files in the executable file is something all game developers are going to find unbelievably useful:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">const int music[] = {    #embed int \"music.wav\" };<\/code><\/pre>\n<p>  <\/p>\n<p>It\u2019s my belief that the Committee has realises that the community knows where their next meeting is being held, and that this preprocessor directive will be accepted without questions.<\/p>\n<p>  <\/p>\n<h3 id=\"farewell-null--or-nullptr-ready-on-the-starting-blocks\">Farewell, NULL \u2013 or nullptr ready on the starting blocks<\/h3>\n<p>  <\/p>\n<p>It would seem that the problematic <code>NULL<\/code> macros are being <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2394.pdf\">replaced<\/a> with the keyword <code>nullptr<\/code>, which will be equivalent to the expression <code>((void*)0)<\/code> and, in the case of type conversion, will have to remain a pointer type. Any use of NULL should be accompanied with a compiler warning:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">\/* I always forget why the cast is necessary. *\/ int execl(path, arg1, arg2, (char  *) NULL);  \/* But happiness is just round the corner *\/ int execl(path, arg1, arg2, nullptr);<\/code><\/pre>\n<p>  <\/p>\n<p>If this example make no sense to you, then take a look at the Linux documentation under <code>man 3 exec<\/code> and you will find your enlightenment there.<\/p>\n<p>  <\/p>\n<h3 id=\"reform-of-error-processing-in-the-standard-library\">Reform of error processing in the standard library<\/h3>\n<p>  <\/p>\n<p>The processing of standard library function errors has been a longstanding problem in C. The combination of unfortunate solutions in various versions of the standard, the conservative stance of the Committee and reverse compatibility issues have all got in the way of finding a solution that suits everyone.<\/p>\n<p>  <\/p>\n<p>And here, finally, is someone prepared to <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg14\/www\/docs\/n2429.pdf\">propose<\/a> a solution for compiler developers, the super-conservative Committee and for us mere mortals:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">[[ oob_return_errno ]] int myabs (int x) {   if(x == INT_MIN ) {           oob_return_errno ( ERANGE , INT_MIN ) ;   }   return (x &lt; 0) ? -x : x; }<\/code><\/pre>\n<p>  <\/p>\n<p>Let me draw your attention to the <code>oob_return_errno<\/code> attribute. This means that the following functions will be generated from this template function:<\/p>\n<p>  <\/p>\n<ol>\n<li>A function returning the structure with an error flag and the result of the work of the (<code>struct {T return_value; int exception_code}<\/code>) function.<\/li>\n<li>A function returning the result of the work of the function, and ignoring possible errors in the arguments, leading to undefined behaviour.<\/li>\n<li>A function terminating execution in the case of an error in the arguments.<\/li>\n<li>A function replacing errno, that is, exhibiting ordinary behaviour.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>The compiler is offered a choice between these options, depending on how the programmer uses a given function:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">bool flag; int result = oob_capture(&amp;flag , myabs , input) ; if (flag) {     abort ();<\/code><\/pre>\n<p>  <\/p>\n<p>In this case, if the function has been carried out properly, this is shown with a flag, while errno is not affected. Function calls saving the error code to the variable, for example, look similar.<\/p>\n<p>  <\/p>\n<p>The actual syntax, it would seem, will yet change, but it is a good thing that the Committee is at least <em>thinking<\/em> in this direction.<\/p>\n<p>  <\/p>\n<h1 id=\"rumours\">Rumours<\/h1>\n<p>  <\/p>\n<p>The author of \u201cEffective C\u201d, along with other Committee members, <a href=\"https:\/\/news.ycombinator.com\/item?id=22865357\">answered<\/a> questions from members of the Hacker News community. Lots of things overlap with what we have noted above. But there are a couple of points which are important for programmers. These have not been formulated as proposals, as such, however Committee members are hinting that work might be underway in these areas.<\/p>\n<p>  <\/p>\n<h3 id=\"typeof-operator\">typeof operator<\/h3>\n<p>  <\/p>\n<p>The <code>typeof<\/code> keyword was <a href=\"https:\/\/gcc.gnu.org\/onlinedocs\/gcc\/Typeof.html#Typeof\">implemented<\/a> a long time ago in compilers and makes it easier to write correct macros. Here is a textbook example:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">#define max(a,b)                                \\     ({ typeof (a) _a = (a);                     \\     typeof (b) _b = (b);                        \\     _a > _b ? _a : _b; })<\/code><\/pre>\n<p>  <\/p>\n<p>Martin Sebor, a developer from Red Hat and a Committee member, maintains that a relevant proposal is already being worked on and will very likely be approved.<\/p>\n<p>  <\/p>\n<p>Keeping my fingers crossed!<\/p>\n<p>  <\/p>\n<h3 id=\"defer-operator\">defer operator<\/h3>\n<p>  <\/p>\n<p>Some programming languages, including ones implemented by Clang and GCC, allow you to bind freed-up resources to the lexical scoping of variables or, to put it more simply, to call given code when the control flow goes outside the scope of the variable.<\/p>\n<p>  <\/p>\n<p>Pure C doesn\u2019t have this option nor ever has, but compilers have been implementing the <code>cleanup(&lt;cleanup function>)<\/code> attribute for a long time:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">int main(void) {    __attribute__((cleanup(my_cleanup_function))) char *s = malloc(sizeof(*s));    return 0; }<\/code><\/pre>\n<p>  <\/p>\n<p>Robert Seacord author of \u201cEffective C\u201d and member of the Committee has <a href=\"https:\/\/news.ycombinator.com\/item?id=22866311\">admitted<\/a> that he is working on a proposal along the lines of the keyword <code>defer<\/code> from Go:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">int do_something(void) {     FILE *file1, *file2;     object_t *obj;     file1 = fopen(\"a_file\", \"w\");     if (file1 == NULL) {       return -1;     }     defer(fclose, file1);      file2 = fopen(\"another_file\", \"w\");     if (file2 == NULL) {       return -1;     }     defer(fclose, file2);      \/* ... *\/      return 0; }<\/code><\/pre>\n<p>  <\/p>\n<p>In this example, the <code>fclose<\/code> function will be called with the <code>file1<\/code> and <code>file2<\/code> arguments, in any case where the program goes outside the body of the <code>do_something<\/code> function.<\/p>\n<p>  <\/p>\n<p>Vive la r\u00e9volution!<\/p>\n<p>  <\/p>\n<h1 id=\"conclusions\">Conclusions<\/h1>\n<p>  <\/p>\n<p>Changes to C are like genetic mutations: they don\u2019t happen often, rarely are viable, but, in the end, they push the evolution forward.<\/p>\n<p>  <\/p>\n<p>The most recent unfortunate changes to C occurred ten years ago. And the most recent quality leap forward in terms of development of the language happened over 20 years ago. And, by all accounts, the members of the Committee have now decided to consider moving forwards in respect of the new iteration of the standard.<\/p>\n<p>  <\/p>\n<p>So, to conclude: use static analysers, run Valgrind as often as possible and try not to write overly-big programs in C!<\/p>\n<p>  <\/p>\n<p><strong>PS<\/strong> I think the \u201cfirst truly interesting book\u201d thing was an overstatement on my part. Someone recommended a book entitled <a href=\"https:\/\/www.manning.com\/books\/modern-c\">\u2018Modern C\u2019<\/a> written by a member of the committee, and that would definitely be worth a read.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/512802\/\"> https:\/\/habr.com\/ru\/articles\/512802\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/7j\/dw\/5r\/7jdw5r9gajn2olyfuypvvwskuve.png\" alt=\"image\" data-src=\"https:\/\/habrastorage.org\/webt\/7j\/dw\/5r\/7jdw5r9gajn2olyfuypvvwskuve.png\"\/><\/p>\n<p>  <\/p>\n<blockquote><p>I strain to make the far-off echo yield<br \/>  A cue to the events that may come in my day.<br \/>  (\u2018Doctor Zhivago\u2019, Boris Pasternak)<\/p><\/blockquote>\n<p>I\u2019ll be honest: I don\u2019t write in pure C that often anymore and I haven\u2019t been following the language\u2019s development for a long time. However, two unexpected things happened recently: \u0421 <a href=\"https:\/\/www.tiobe.com\/tiobe-index\/\">won back<\/a> the title of the most popular programming language according to TIOBE, and the first truly <a href=\"https:\/\/nostarch.com\/Effective_C\">interesting<\/a> book in years on this language was published. So, I decided to spend a few evenings studying material on C2x, the future version of C.<\/p>\n<p>  <\/p>\n<p>Here I will share with you what I consider to be its most interesting new features.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-390778","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/390778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=390778"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/390778\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=390778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=390778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=390778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}