Register - Login
Views: 95765798
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - Stats - Latest Posts - Color Chart - Smilies
11-15-18 07:50:19 AM

Jul - Computers and Technology - Cool C++ stuff coming up New poll - New thread - New reply
Next newer thread | Next older thread
marrub

Level: 11


Posts: 26/44
EXP: 5018
For next: 967

Since: 01-23-18

Pronouns: they/she
From: hell

Since last post: 4 days
Last activity: 3 hours

Posted on 08-22-18 05:31:06 PM Link | Quote
Been getting back into programming C++ recently, reading up on it and such, and there is some really cool stuff coming up in C++2a/C++20. C++17 was already a great improvement to the standard (fold expressions, nested namespaces, constexpr if, structured bindings...) but they've finally implemented some of the things I've wanted out of the language for just about ever.

Here's an exhaustive list of stuff currently merged into the working draft that I like (for more check here):


  • P0683R1 Default member initializers for bit-fields
    This is rather important if your code style uses default initializers. Bit fields are important if you want to have a set of structured flags without wasting tons of space having individual booleans or destroying sanity with a manual bit-field.

    int const b = 0;
    struct S {
    int x1 : 8 = 42;
    int x2 : 8{42};
    int y3 : (true ? 8 : b) = 42;
    };


  • P0306R4 __VA_OPT__ - comma elision in variadic macros
    Ever since C99 introduced variadic macros (#define x(...) __VA_ARGS__) there's been an issue with passing nothing in the argument list where the expanded text puts a comma expecting there to be something.

    As a practical example, say you have a "print" macro which calls printf with the name of the function that's calling it as an argument:

    #define print(fmt, ...) std::printf("%s: " fmt, __func__, __VA_ARGS__)

    Seems pretty straightforward. You can then invoke it like this:

    int main()
    {
    print("escape %i", 500); // prints out "main: escape 500"
    }

    But wait, what if you don't have any argument to pass, and the format is plain?

    print("escape 500"); // ERROR: expected primary-expression, got ")"

    Oh. Right. That expands to std::printf(..., __func__, ). This issue has existed for literal decades now. Hopefully __VA_OPT__ gets merged back into the C standard at some point, but anyhow, this is how you'd fix it in C++2a:

    #define print(fmt, ...) std::printf("%s: " fmt, __func__ __VA_OPT__(,) __VA_ARGS__)

    Simple and effective, if no arguments are provided it won't expand anything in __VA_OPT__. Horrifyingly, you could use this to make proper recursive macros and probably even implement some kind of Lisp variant in it. But please, please don't.

  • P0329R4 Designated initializers
    Much the opposite of the previous bullet-point, designated initializers have existed in C for literal decades and have never made their way back into C++. This is for a good reason; the rules for them are horrifyingly complex and don't mesh with C++ at all. Thankfully, Vulkan exists now, and abuses the fuck out of designated initializers. So now that there's finally a push to add them, the committee has figured out a decent compromise between C's extremely powerful and elder-god-invoking designated initializers and C++11's new brace style initializer syntax.

    Here's an example of what you can do with it:

    struct point {float x, y, z;}
    point p{.x = 3.0, .y = 4.0};
    distance({.x{}, .z{5.0}}, p);


  • P0734R0 Concepts
    These are rather difficult to explain because the TS is vast and adds a lot of new language features, but essentially they're adding constraint-based meta-programming to C++ because people have for years been doing that with really bad template madness.

  • P0515R3 Consistent comparison
    Also known as the fish-hook operator, a way of doing relative logical comparison without writing a ton of other operator overloads when really all you needed was a way of doing subtraction in a comparison context. Especially important for string comparison in the future, maybe?

  • P0840R2 True empty objects
    Currently in C++ a structure with no non-static data members such as struct a {}; is not actually empty. Because you must be able to take the address of an object anywhere at any time, the standard makes it the size of a pointer, even though there's no actual data in it. This has caused many issues throughout the years in both user-land and the standard template library, for instance making it so std::vector has to inherit from the default allocator because otherwise it'll be wasting a significant amount of space and probably drive the committee to the brink of insanity.

    I'm honestly not sure why this wasn't added sooner instead of implementing all kinds of workarounds for it within the standard library when the change is something that all compilers already support and only took a paragraph or so of wording but oh well.

  • P0542R5 Contract-based programming
    This is sort of gimmicky but nonetheless I know lots of people are going to use it.



There's also been all kinds of improvements to range-for, lambdas, structured bindings, and more. These are just some of the highlights for me. (I don't even know if anyone else here uses C++ but this stuff is cool and makes me feel much less on the fence about it.)
andlabs
Member
Level: 35


Posts: 286/290
EXP: 277766
For next: 2170

Since: 03-18-10

From: United States

Since last post: 65 days
Last activity: 2 days

Posted on 08-22-18 06:16:53 PM (last edited by andlabs at 08-22-18 06:23:16 PM) Link | Quote
Originally posted by marrub

  • P0329R4 Designated initializers
    Much the opposite of the previous bullet-point, designated initializers have existed in C for literal decades and have never made their way back into C++. This is for a good reason; the rules for them are horrifyingly complex and don't mesh with C++ at all. Thankfully, Vulkan exists now, and abuses the fuck out of designated initializers. So now that there's finally a push to add them, the committee has figured out a decent compromise between C's extremely powerful and elder-god-invoking designated initializers and C++11's new brace style initializer syntax.

    Here's an example of what you can do with it:

    struct point {float x, y, z;}
    point p{.x = 3.0, .y = 4.0};
    distance({.x{}, .z{5.0}}, p);




finallyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

watch them remove it again because constructors

(and too bad I still need to support C++11)

EDIT wait it doesn't include arrays, ugh ("Array designators conflict with lambda-expression syntax") So much for my enthusiasm
Next newer thread | Next older thread
Jul - Computers and Technology - Cool C++ stuff coming up New poll - New thread - New reply




Rusted Logic

Acmlmboard - commit 220d144 [2018-11-04]
©2000-2018 Acmlm, Xkeeper, Inuyasha, et al.

26 database queries.
Query execution time: 0.202668 seconds
Script execution time: 0.010555 seconds
Total render time: 0.213223 seconds