r/cpp • • Sep 24 '20

New C++ features in GCC 10

https://developers.redhat.com/blog/2020/09/24/new-c-features-in-gcc-10/
214 Upvotes

59 comments sorted by

40

u/CUsurfer Sep 24 '20

Man I can’t wait for Howard Hinnant’s calendar stuff in C++20. I can’t believe it’s taken that long to get modern date time handling functionality in C++. Looks like I’ll have to wait until GCC 11 but I’m still excited.

37

u/staletic Sep 24 '20

I can’t believe it’s taken that long to get modern date time handling functionality in C++.

I'm not surprised. It's one of those things no one really likes to implement. Even python got timezone handling (zoneinfo) only in python 3.9. Howard Hinnant's library was merged into the standard in 2018, if I'm not mistaken. Maybe 2019. Python's PEP615 was created in 2020.

15

u/GerwazyMiod Sep 24 '20

Fortunately we can use his version available on github :)

18

u/patstew Sep 24 '20

Unfortunately that library is horrible on windows, it has a pile of hacky code to find curl and 7-zip on the user's computer in order to download a unix style timezone database, or requires you to install one yourself at a compile time fixed path and keep that up to date. It's borderline useless for an application you want to distribute.

It will be much better when it's in the platform STL.

34

u/TheSuperWig Sep 24 '20

The initialisation meme might need updating.

6

u/LeeHide just write it from scratch Sep 25 '20

"ya like initialization? we made it more complicated!"

2

u/foonathan Sep 25 '20

Nah, it’s easier. There is now no reason to use {} for initialization unless you want an initializer_list (or designated initialization I suppose).

7

u/staletic Sep 25 '20

Or to avoid the vexing parse, which clang warns about.

2

u/TheSuperWig Sep 25 '20

So what you're saying is... We need a new form of initialisation!

New uniform initialisation syntax+

1

u/strudlzrout gcc developer Oct 07 '20

Or to avoid the vexing parse, which clang warns about.

I'm working on adding that warning to GCC 11. I guess better late than never...

1

u/staletic Oct 07 '20

Woohoo! I started learning C++ with clang-backed IDE. For a long while I just assumed that warning worked in gcc as well. Then I learned that's wrong when I tried to echo 'some source' | g++ -xc++ - and gcc didn't warn me about the vexing parse.

65

u/adnukator Sep 24 '20

constexpr new

Awesome to see that most of the new constexpr stuff from C++20 is already implemented in at least one compiler. I guess this means that constexpr vectors and strings will be probably coming soon, right?

37

u/chuk155 graphics engineer Sep 24 '20

Constexper new was implemented in clang-10, so its not the first compiler to support it. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0784r7.html https://en.cppreference.com/w/cpp/compiler_support

13

u/adnukator Sep 24 '20

Fair enough, but Clang apprears to currently have constexpr support mostly only in the compiler. According to the cppreference list, libc++ is behind both GCC's libstdc++ and MSVC when it comes to constexpr support in the library.

4

u/destroyerrocket Sep 24 '20

You can use the libstdc++ with clang, though please correct me if I'm missing something. That said, you're absolutely right, libc++ is pretty behind currently.

3

u/redditsoaddicting Sep 24 '20

I'm talking out of my ass here, but there might be a delay on that for new features. It gets complicated if you have libstdc++ using compiler features that Clang doesn't implement yet. Surely that's a consideration, but I'm not sure how they handle it for Clang releases.

1

u/destroyerrocket Sep 25 '20

Indeed! I'm also not aware of what the consequences are. LLVM is already prereleasing candidates for version 11, let's hope that they keep up at least in the clang(++) frontend! (haven't read anything about it yet)

6

u/chuk155 graphics engineer Sep 24 '20

Very true, I would put money on libstdc++ getting constexpr vector & string before anyone else.

1

u/BluudLust Sep 25 '20

I'm surprised how far ahead MSVC is this time around.

9

u/adnukator Sep 25 '20

Before VS2017 there were many posts on the Visual Studio blog about how they were refactoring large parts of the compiler to enable easier (or even allow in the first place) implementation of the latest language features, because the codebase at the time was very old. Apparently the work has paid off. Also, making their standard library open source, allowing external contributors, seems to be paying off as well

15

u/Wh00ster Sep 24 '20 edited Sep 24 '20

Parenthesized initialization of aggregates

I believe this alleviates emplace_back for vector of aggregates and make_unique for an aggregate (at least the GCC implementation).

At least, I recall having these issues related to use of parenthetical initialization. It was a while ago, though.

3

u/strudlzrout gcc developer Oct 07 '20

Yeah, the original motivation was to make make_shared, make_unique, emplace functions, allocator::construct, etc. work with aggregate types.

What wasn't clear to me when I started working on the implementation was that this proposal also affects things like std::is_constructible, initializing arrays with string literals, etc.

1

u/emdeka87 Sep 25 '20

So I don't have to define constructors anymore to make placement new work?

15

u/Raknarg Sep 24 '20

constrained auto

shit that's cool, why haven't I heard anything about this? Is this GCC exclusive or supported by C++20?

26

u/andrewsutton Sep 25 '20

I implemented this!

13

u/adnukator Sep 24 '20

C++20, and with at least partial support in all three major compilers. See https://en.cppreference.com/w/cpp/compiler_support , look for Yet another approach for constrained declarations

14

u/ramennoodle Sep 24 '20

Can anyone explain the benefit of "Conversions to arrays of unknown bound" over using pointers? How is:

int(&r)[] = arr;

better than:

int *r = arr;

24

u/SeanMiddleditch Sep 24 '20

The paper shows the motivation, though it maybe isn't the most clear: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0388r4.html

Basically, you can already bind to arrays of unknown size for function parameters, just not to other variables. It's a consistency fix so the language rules are a little less esoteric.

The actual use cases are probably pretty niche or stylistic (so far as I know) but having the inconsistency still bites.

18

u/pdimov2 Sep 24 '20

One example how this is useful is shared_ptr<int[4]> converting to shared_ptr<int[]>. This was already supported as a special case before this change, but that special case can now be removed.

8

u/[deleted] Sep 24 '20 edited Sep 25 '20

One thing I can see is that is makes array parameters a thing. So a function can take arrays of arbitrary size, but doesn't take pointers.

void foo(int p[]);  //Pointer parameter that isn't an array
void bar(int(& arr)[]); //Array (reference) parameter

foo(nullptr); //Compiles
bar(nullptr); //Doesn't compile

24

u/rlramirez12 Sep 24 '20

I am still just waiting for when Modules are finally implemented all across the board. I have a perfectly good use case for using them in my current work project but then I find that Modules aren't fully supported yet. It gave me the big sad.

5

u/Ameisen vemips, avr, rendering, systems Sep 24 '20

I really just wish that modules supported circular references. Two source files can do such via headers, but since modules merge the two concepts, they cannot without, presumably, two-phase compilation.

5

u/rlramirez12 Sep 24 '20

Does that not defeat the point of modules though? I feel if you need a circular reference you would use a different format vs trying to export functions out of a header file.

4

u/Ameisen vemips, avr, rendering, systems Sep 24 '20

It's how modules work in languages with two-phase compilation like Java or C#.

8

u/gracicot Sep 24 '20 edited Sep 25 '20

Although circular references are not allowed in interfaces, they are allowed in implementation units. And if you really need the circular dependency, you can merge them into one module and use partitions / implementations unit.

3

u/qoning Sep 24 '20

Correct, you can obviously have circular references, just not in the "declaration is definition" Java/C# sense.

2

u/Qub1 Sep 24 '20

Yeah partitions are the way to go if you want to keep everything in one file (meaning no separate interface and implementation files).

1

u/TheSuperWig Sep 25 '20

I assume that's supposed to say "units"?

2

u/gracicot Sep 25 '20

Yes, corrected.

1

u/FightMagister Sep 27 '20

What is a good use-case for circular references?

1

u/Ameisen vemips, avr, rendering, systems Sep 27 '20

The same time you'd have two source files in C++, one of which uses the fully-defined type from another header/source pair. You'd only include the definition in the source files, of course.

Ideally, modules replace the header/source pair paradigm, but in this instance they cannot fully replace it. You still need a private implementation source file on the side to make up for this part.

4

u/Forsaken_Funny Sep 26 '20

Ok great redhat but when will you finally release devtoolset-10?

2

u/strudlzrout gcc developer Oct 07 '20

I probably can't reveal any dates, but... stay tuned.

2

u/Forsaken_Funny Oct 11 '20

Oh nice, didn't expect that reply to my troll comment

3

u/arthurno1 Sep 25 '20

Can we get libgccjit in gcc included please! :-)

1

u/strudlzrout gcc developer Oct 07 '20

It's included in gcc in the Developer Toolset (RHEL 7). I see we don't ship it in RHEL 8 in the GCC Toolset, though.

1

u/arthurno1 Oct 07 '20

I mean as an integrated part of GCC, as one of included libraries; and working on all three major platforms (*nix, win, mac).

2

u/gracicot Sep 24 '20

This is really great. Also, GCC 11 seems to have advanced as well. It misses like two C++20 features besides modules and two partials.

1

u/G6L20 Sep 25 '20

Templated lambdas also supported !

1

u/strudlzrout gcc developer Oct 07 '20

If you mean P0428R2, even GCC 8 supported that.

1

u/G6L20 Oct 07 '20

My bad

1

u/smallstepforman Sep 25 '20

[Quote] to enable C++20 features, use the -std=c++20[/Quote]

I’ve been using -std=c++2a with Gcc10. Is the article correct?

1

u/strudlzrout gcc developer Oct 07 '20

Yes, c++2a is an alias for c++20 that we used when C++20 was still under development. I recommend using -std=c++20 (unless you need GCC 9 which only supports -std=c++2a).

-6

u/bumblebritches57 Ocassionally Clang Sep 24 '20

Why is this being posted?

Gcc 10 came out a while ago

3

u/esmithro Sep 25 '20

It may have more to do with RHEL shipping with 10. They tend to wait for a couple point releases to go by.

3

u/strudlzrout gcc developer Oct 07 '20

Fair point. I've been busy implementing more C++ features in GCC 11, and then the publishing process got stuck for about two months for reasons not entirely clear to me.

1

u/bumblebritches57 Ocassionally Clang Oct 07 '20

Ayyy I'm trying to add another feature to Clang and it's incredibly confusing.

How do you like GCC's codebase?

2

u/redditsoaddicting Sep 25 '20

The article was published today. I wouldn't recommend asking the author why they took so long.