r/programming • • Sep 24 '20

New C++ features in GCC 10

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

80 comments sorted by

35

u/nandryshak Sep 24 '20

C++ is actually a pretty nice language. It has a lot of nice features, it's fast, popular, lots of libraries, great tooling, cross-platform GUIs. Despite being a bit ugly, if they sped up compile times and put in good error messages instead of incomprehensible gibberish it'd be a fantastic language (provided you use smart pointers and other more modern features).

32

u/lookatmetype Sep 24 '20

If there was a cargo for C++, I would prefer it over Rust any day. What a lot of people who don't understand Rust's popularity don't get it is how easy it is to get a project setup and start using packages. Doing that in C++ is a nightmare outside of vcpkg, and even there its not great.

13

u/Jump-Zero Sep 24 '20

There's been a bunch of times that I get home super inspired to start a C++ project. By the time I finish setting up the external libraries, its been 2 hours, I'm exhausted, I have made zero progress on the project itself, and I never revisit it. I can build some really crazy shit with node in 2 hours in comparison.

6

u/emelrad12 Sep 25 '20

I have a single project that has my important dependencies pre installed so every time I start I can just copy it.

1

u/ArkyBeagle Sep 25 '20

I'd at least do some exercises to understand how the workflow for integrating C++ libraries works. Where's it's frustrating is within the two IDE I have tried; in makefiles it seems like like less trouble. For that, I consider the basic use case analysis for the IDE to have failed.

I tried vcpkg for a while and... nope nope nope nope.....

Source: I "grew up" programming with no internet connection.

1

u/shawnwork Sep 27 '20

This is actually a deciding factor.

1

u/pjmlp Sep 25 '20

Yeah, but then since cargo only deals with source, one can go prepare a meal while the world compiles.

vcpkg while not perfect is quite sweet point.

0

u/[deleted] Sep 24 '20

[deleted]

25

u/moon-chilled Sep 24 '20

there are several

And therein lies the problem.

3

u/[deleted] Sep 25 '20

I don't know about that. You have to remember that C++ comes from a time before package managers were even a thing and historically has run in a lot more environments than a newer language like Rust has. (although Rust is coming along nicely)

17

u/Sakki54 Sep 24 '20

There is no package manager for C++ where I can add any C++ package and have it “just work”, by adding the name and a version or git url to a config file.

16

u/UNN_Rickenbacker Sep 24 '20

Agree on all things except tooling. Outside of Visual Studio it's a fucking nightmare to work with. Even vcpkg requires quite a bit of hackiness to get working, and it can only count 600 packages amongst itself.

If CPP had anything like npm it would be even more popular.

CPP has one more negative: It somehow became fashionable to write libraries with abysmal documentation. Hell, even microsoft's official CPPRestSDK does only have doxygen generated docs and most of the functions and classes are described by a single sentence.

0

u/[deleted] Sep 24 '20

[deleted]

9

u/[deleted] Sep 24 '20

Impressive growth but that's still not many. C must have more libraries than Rust but Crates.io has 47k packages.

→ More replies (3)

7

u/L3tum Sep 24 '20

Error messages and reflection is what really throws me off.

CMake is also something I never really got to make any gains in. Especially since it's not really cross platform (you'd be hard-pressed to find any Windows PC with make installed).

Coming from C#, errors, templates and reflection are definitely the biggest pain points though.

16

u/lightmatter501 Sep 24 '20

Cmake can output visual studio projects for MSVC to compile.

5

u/vazgriz Sep 25 '20 edited Sep 25 '20

Visual Studio can also open CMake projects directly, without needing to generate .sln files.

4

u/L3tum Sep 24 '20

See, that's something I've never even heard about. Thanks

3

u/hyvok Sep 24 '20

Or just install Ninja and use the output generator for that.

3

u/pjmlp Sep 25 '20

What version of Studio are you using?

Visual Studio has native support for CMake since 2017, naturally the best experience in on 2019.

0

u/meneldal2 Sep 25 '20

I have to admit C# reflection is quite powerful. There's no current plans for this kind of runtime reflection in C++ (it is a performance killer so that's unlikely to be happening), however static reflection is being actively worked on. I really wish it can make it into C++23.

Static reflection can be even more powerful, as you can generate code at compile time in a clean way instead of using macros. It would be able to deal with all the boilerplate you need to deal with and just generate the code you need. Right now the best thing you can do is using concepts to ensure you're not missing it, not generate it.

Error messages have improved greatly, concepts definitely help if the libraries you use make use of them instead of 98-style SFINAE (no saving you from those error messages). It removes a lot of template madness.

1

u/flatfinger Sep 25 '20

It would be helpful if C or C++ would include features to auto-generate program-readable information for particular structures. A function to render to JSON an object pointed at by a `void*` wouldn't be able to perform such rendering simply given the pointer, but there shouldn't be any particular difficulty supporting something like: renderToJson(outFile, *myThing, __getStructFormat(typeof *myThing, myFormatStyle)); where myFormatStyle could be a macro specifying what information will be needed and how it should be formatted. The format would be processed at compile-time to produce a pointer to a string or other blob which would then be used by the rendering function.

1

u/meneldal2 Sep 26 '20

Serialization is no problem if you have compile time reflection, you'd probably use an interface like ISerializable that requires to implement a serialization function.

It's possible to do many things with macros, but the syntax is horrible and it's nearly impossible to debug.

1

u/ArkyBeagle Sep 25 '20

I use quite a bit of non-smart-pointer C++ ( from signals processing stuff ) and it's not that bad.

put in good error messages instead of incomprehensible gibberish

There's usually a line number in the spoo....

2

u/nandryshak Sep 25 '20

There's usually lots of line numbers ;)

-25

u/xzaramurd Sep 24 '20

Might be a good idea to remove raw pointers all together or restrict their usage by default.

12

u/[deleted] Sep 24 '20

Good or bad aren’t raw pointers one of the defining characteristics of C++?

11

u/ismtrn Sep 24 '20

Technically the entire specification is the defining characteristics of C++, so... yes.

6

u/nandryshak Sep 24 '20

Nahh definitely not. Maybe add -Wraw-pointers. But the backwards compatibility is especially important.

2

u/ketzu Sep 24 '20 edited Sep 24 '20

It's debatable if it would be a good idea or not. (I don't really have an opinion on it) But, it will essentially never happen in any way, because it would break nearly all c++ software. Backwards compatability is one of the things that keeps c++ alive.

(Someone more famous once said something along the line of "Noone says 'most of the c++ code is yet to be written' anymore")

→ More replies (2)

0

u/the_gnarts Sep 24 '20

restrict their usage by default

C++ needs an unsafe keyword?

0

u/[deleted] Sep 24 '20

[deleted]

9

u/lookatmetype Sep 24 '20

Not if you use template heavy modern C++. Gotta recompile everything on every edit!

1

u/caroIine Sep 25 '20

I use templates and concepts everywhere in my project and I have yet to see this “slow compile time”. But that’s might be because I use multicore cpu so when I hit rebuild, 16 source files are compiling at once.

1

u/emelrad12 Sep 25 '20

Do you use a template monstrosity like eigen?

3

u/serviscope_minor Sep 25 '20

Without adding my thoughts on eigen per-se, I disagree. If you're frequently editing the headers and recompiling the entire project then you're probably doing something wrong. Using eigen can make some individual source files take a long time to compile, but the edit-compile-run cycle isn't too bad since it shouldn't be rebuilding everything every time.

Also, you can always split the source file up a bit and/or rewrite some small core pieces in non eigen, while keeping the majority quick do develop and easy to verify the correctness.

1

u/emelrad12 Sep 25 '20

Well it isn't bad with precompiled headers and some other options, the builds are fast, except when you shove it into nvcc, then shit hits the fan.

2

u/caroIine Sep 25 '20

I use glm for linear algebra.

12

u/[deleted] Sep 24 '20

Honestly I can't say I have ever found a bug that was caused by C++'s proliferation of features. Ok maybe one, caused by inadvertant lambda captures.

On the whole the additional features allow you to write code that leads to fewer bugs because you can express more stuff that the compiler can reason about. For example Concepts will add a ton of complexity, but the entire point is that they help you avoid certain classes of bugs.

C might be simpler for not having smart pointers, but do you really think that means it has fewer null pointer dereferences and use-after-frees?

If you structure your project well, you only have one slow compile

Well, and your CI is slower, and you have a huge wait every time you have to do a clean build (which is definitely more than once), and final link times can be very slow anyway.

Incremental compilation helps but it doesn't completely excuse C++'s build times.

0

u/[deleted] Sep 28 '20

[deleted]

1

u/[deleted] Sep 28 '20

[citation needed]

Only someone who had never actually used C's string library and std::string would need a citation for that. Or malloc/std::vector or std::unique_ptr etc.

→ More replies (2)

4

u/LAUAR Sep 24 '20

You can just ignore many of C++'s features. It might even be what most big projects in C++ do.

5

u/serviscope_minor Sep 25 '20

It's not so much ignore, as not use when not appropriate.

For example, unless you're dealing with hardware memory mapped IO or something like unix signals, you need never touch volatile.

If you're not implementing an allocator or container or something similar, then you never need touch placement new. If you're implementing these often, then... why?

If you're not working on something well modelled by inheritance and virtual functions, then you won't be doing much derivation, virtual methods, and won't be anywhere near virtual inheritance.

There's the whole C standard libarary, if you're not interfacing with some C things, or porting some C code, you won't need much of it.

Operator overloading is fantastic, and is great for things like strings, maths libraries, and streams. You're probably using it, but (other than stringification) unless you're writing your own EDSL, you won't need or even want to overload operators.

And so on.

Don't ignore features, or ban them outright, just use them when appropriate. Telling people to ignore features is like telling me to ignore my router plane. I mean sure, I can physically work without one (I didn't used to own one), but for its intended purpose, it is quicker, faster, neater, more accurate and more repeatable than doing it manally with a chisel and drill. And since it flattens things more cleanly and accurately if it's involved in a glue joint, the joints are stronger. It would be silly to ignore the presence of such a niche tool, but it would also be insane to use it for jointing even though with the aid of a good square and a couple of straight edges, I could if it was my only cutting tool.

1

u/pjmlp Sep 25 '20

Languages like Object Pascal also allow the same regarding fast code, while offering very fast compile times.

I am looking forwards to modules eventually improving the whole matter.

1

u/[deleted] Sep 24 '20

This.

0

u/[deleted] Sep 24 '20

[deleted]

15

u/TalesM Sep 24 '20

Well, at least over the sizes of data types it now has int8_t, int16_t and so on for some time. Particularly I use char if it is something related to strings and int8_t otherwise. Similarly I use other intX_t when the I care for the day size (like serialization stuff) otherwise I use int as a meaning to say "whatever is easier on the machine".

I just don't use long or short anymore as they just confuse everything and are portability killers and just leftovers maintained for retro compatibility reasons.

0

u/[deleted] Sep 24 '20

[deleted]

3

u/AKostur Sep 24 '20

See also std::byte (for a 1-byte data type, C++17), and modules (in C++20, for your include concerns).

6

u/evaned Sep 25 '20 edited Sep 25 '20

std::byte (for a 1-byte data type, C++17)

Speculating, but I would guess that TalesM wouldn't be happy with std::byte. It's deliberately made without arithmetic operations, so you can't actually use it as a one-byte integer in a reasonable way.

That's a deliberate decision, but it's one that doesn't leave people who do want a single-byte int happy.

Edit: You can't even cout << a std::byte object, which to me is maybe the primary place that char shows its characterness vs byte-sized int-ness.

1

u/flatfinger Sep 26 '20

Consider the following function:

void *scrub_pointer(void *p)
{
  void *q;
  char *pp = (char*)&p;
  char *qq = (char*)&q;
  for (int i=0; i<sizeof p; i++)
  {
    unsigned char b1 = pp[i], b2=0;
    while(b2 < b1)
      b2++;
    qq[i] = b2;          
  }
  return q;
}

Is the returned pointer derived from p? How could a compiler be expected to tell? It's assembled from bytes whose values match those of p, but none of the values are actually "copied" from the bytes of p.

I think that upholding the kind of reasoning the "abstract byte" seeks to facilitate in all corner cases would require going further and requiring that all operations on bytes comprising pointers use byte-copy operations that never split the bytes comprising pointers, since otherwise it would be impossible for a compiler to reason about operations that store the bytes comprising pointers at arbitrary spots in the array, then permute them in some fashion, and then re-assemble the bytes in the proper order. Still, if one was trying to design a "sand-boxed" version of the language, having an abstract byte concept would be useful.

1

u/ArkyBeagle Sep 25 '20

#include <stdint.h>

11

u/okovko Sep 24 '20

Datatypes are random sizes

... stdint. Serialization for IPC and networks. This is a non-problem.

3

u/evaned Sep 25 '20

o you make a program, thinking that longs are 4 bytes, and some poor sap is running your program on like linux, suddenly those longs become 8 byte and everything is fucked hard. (same with long long [emph mine])

I don't know any platform on which long long is not 64 bits.

In fact, in practice unless you're doing embedded development, long (and unsigned long) is likely to be the only type that changes size.

2

u/serviscope_minor Sep 25 '20

I don't know any platform on which long long is not 64 bits.

I was about to get pedantic, but wow even on AVR-gcc sizeof(long long) is 8, making it 4 times the size of size_t!

2

u/evaned Sep 25 '20

The standard guarantees a minimum size of 64 bits1. Some embedded platforms do have usual implementations that deviate from the standard, but if you hit one of those then that implementation is really where you should put the blame rather than on C. One could have more than 64 bits, but like I said I don't know anyone who does.

1 Technically it guarantees a minimum numeric range of at least [-263+1,263-1] for long long and [0,264-1] for unsigned long long, but assuming you're on a platform that represents numbers in binary then that obviously translates to a minimum 64 bits.

0

u/[deleted] Sep 25 '20

[deleted]

4

u/[deleted] Sep 25 '20

The lack of guarantee is for the sake of portability

0

u/[deleted] Sep 25 '20

[deleted]

3

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

How do you use it in practice and/or why?

Because hardware architectures exist that are vastly and fundamentally different from x86 or ARM.

Some have very strange semantics that are difficult to intuit, especially if you haven't actually learned the necessary concepts prior.

Ergo, writing assembly on some architectures is much more complex than writing a program in something like MIPs or ARM.

Imagine an architecture, for example, that has process scheduling built in to the hardware, or 3 separate registers that you can use to write to and read from the stack (no direct memory access).

There's always a reason why these things are the way they are. It has nothing to do with negligence or incompetence.

I.e., Inconvenience or awkwardmess does not imply bad or wrong.

A lot of people make assumptions based on propaganda and parroting information; you should put effort into being well informed.

We're supposed to be engineers, not people who rely on their personal bias or preference to formulate opinions that are based totally on ignorance.

That inevitably just creates problems.

1

u/flatfinger Sep 25 '20

Because hardware architectures exist that are vastly and fundamentally different from x86 or ARM.

A good language should not make programmers bend over backward to accommodate quirks or limitations of platforms upon which their code will never be run.

If there is less than a 5% chance that a piece of code will ever be called upon to run on anything other than a 32-bit octet-based microcontroller, that means there's a 95% chance that any effort spent trying to make the code usable on other platforms will add zero value. If one can make the code directly usable on other platforms with minimal added effort or other downsides, that's well and good. If, however, the most practical way for a program to accomplish some tasks on all of the machines where it will be be used would exploit some features that is common to those machines, a quality implementation that seeks to be maximally suitable for performing such tasks on those machines should support the features in question, without regard for whether the Standard would require that it do so.

What made C useful in the days before the Standard was that rather than trying to have all implementations use one abstraction model, implementations targeting different platforms and purposes would use abstraction models which were tweaked to be maximally suitable for those platforms and purposes. There wasn't any official "standard" that specified that running a construct like *(unsigned short*)0x12345678 = 0xABCD; on a platform that specifies a representation-preserving conversion between integers and physical addresses will store the value 0xABCD to physical address 0x12345678, but compilers intended to be suitable for low-level programming on such platforms would process such constructs in that fashion anyhow. Such code would be meaningless on platforms that don't specify a consistent mapping between pointers and integers, but so what? If twelve machines which use such a mapping also specify the same useful behavior for that address, there's no reason the construct shouldn't be freely portable among implementations targeting those twelve machines.

Unfortunately, the C and C++ Standards have served to promote needless incompatibility among implementations which target the same platforms, by allowing compiler writers to claim that any programs their compilers don't process meaningfully are "broken", rather than recognizing that the programs are intended for use with compilers that are suitable for low-level programming on their target platforms, and that failure to work with compilers that aren't designed to be suitable for such purposes is not a defect.

1

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

Because hardware architectures exist that are vastly and fundamentally different from x86 or ARM.

A good language should not make programmers bend over backward to accommodate quirks or limitations of platforms upon which their code will never be run.

In what way do you have to bend over backwards, especially if you know the code is never going to be ran on an obscure architecture?

We're also discussing a standard.

If there is less than a 5% chance that a piece of code will ever be called upon to run on anything other than a 32-bit octet-based microcontroller, that means there's a 95% chance that any effort spent trying to make the code usable on other platforms will add zero value.

Yes. This is immaterial though.

If, however, the most practical way for a program to accomplish some tasks on all of the machines where it will be be used would exploit some features that is common to those machines, a quality implementation that seeks to be maximally suitable for performing such tasks on those machines should support the features in question, without regard for whether the Standard would require that it do so.

This is irrelevant. Decisions aren't made that way, because it's impossible to determine long term side effects like this.

What made C useful in the days before the Standard was that rather than trying to have all implementations use one abstraction model, implementations targeting different platforms and purposes would use abstraction models which were tweaked to be maximally suitable for those platforms and purposes. There wasn't any official "standard" that specified that running a construct like *(unsigned short*)0x12345678 = 0xABCD; on a platform that specifies a representation-preserving conversion between integers and physical addresses will store the value 0xABCD to physical address 0x12345678, but compilers intended to be suitable for low-level programming on such platforms would process such constructs in that fashion anyhow. Such code would be meaningless on platforms that don't specify a consistent mapping between pointers and integers, but so what? If twelve machines which use such a mapping also specify the same useful behavior for that address, there's no reason the construct shouldn't be freely portable among implementations targeting those twelve machines.

The standard is nothing more than a universal catch all. It specifies semantics that remain consistent across platforms and those which don't: the implementation is free to define their own gaps and note what those are.

Unfortunately, the C and C++ Standards have served to promote needless incompatibility among implementations which target the same platforms, by allowing compiler writers to claim that any programs their compilers don't process meaningfully are "broken", rather than recognizing that the programs are intended for use with compilers that are suitable for low-level programming on their target platforms, and that failure to work with compilers that aren't designed to be suitable for such purposes is not a defect.

Can you actually list cases of this?

I have a difficult time thinking someone wrote, say, firmware for some obscure architecture that was compiled by a GCC backend, and then a Clang programmer went out, attempted to compile the firmware for own competing implementation, and then wasn't able to deal with the issues they needed to fix in order to ensure it was processed effectively.

I also can't imagine them going to the committee and complaining, unless in this example Clang was fully standards compliant and GCC's backend contradicts the standard.

But even then, the issue should be that GCC's implementation should only deviate where necessary in order to ensure that the firmware compiles successfully.

So, if there's an issue, it should be an update to the standard.

Then the vendors define their own constraints.

→ More replies (1)

4

u/evaned Sep 25 '20

It's not weird if you think about C being a relatively low-level languages with goals providing low level, high performance access to the hardware. Register sizes and such change by processor and platform, so it's only natural that the sizes of at least some of its primitive types also change.

2

u/meneldal2 Sep 25 '20

Back in the time of C, there were many architectures and it wasn't possible to say that ints must be a certain size.

I think at some point C++ could have added native types (not things you have to include) with short names like Rust, but too many people were like "but it'll break my code" (which is only true if you defined those as different types or named variables like this, which should be a crime either way), so it never happened.

0

u/[deleted] Sep 25 '20

[deleted]

1

u/meneldal2 Sep 25 '20

There are still plenty of people who try to push in breaking changes, it just doesn't go over very well.

0

u/[deleted] Sep 25 '20

Your complaints about primitive datatype sizes are shortsighted

7

u/Wind_Lizard Sep 24 '20

Hopefully I will be able to use this in 2030 at least. Currently stuck with gcc 4.4 :(

3

u/theMonoid Sep 25 '20

I actually make my company(a top 10 tech company in China )upgrade from gcc 4.8 to gcc10 in a year recently, yes, that’s the most exciting job I have done in my whole career

1

u/Wind_Lizard Sep 25 '20

Good job! I am counting days until EOL of RHEL6 end of this year so we can finally move to 4.8.5. That was the best I could convince my boss on :(

2

u/theMonoid Sep 28 '20

Actually we are currently using the CentOS6, and two years ago CentOS 4 was everywhere. Keep tens of thousands of servers up to date is an impossible task, so we take another way: we build our own tool-chain including kernel/glibc/binutils/gcc/clang etc, make the tool-chain self contained and deploy it on every server, that's why we can have clang10/gcc10 on CentOS 4 and CentOS 6, that's not a perfect solution, still it make us benefit from the latest C++ standard.

2

u/stbrumme Sep 25 '20

RHEL6 / CentOS 6?

2

u/Wind_Lizard Sep 25 '20

Yes, Both actually

-3

u/Puzzleheaded-Wolf-37 Sep 24 '20

C++ became more and more complex :(

7

u/salvoilmiosi Sep 24 '20

you are not obliged to use all of the features :)

2

u/DowsingSpoon Sep 25 '20

No, but you are obliged to understand all of them, and their interactions...

2

u/Hacnar Sep 25 '20

No, there are many arcane c++ constructs that I don't use or understand, and I have yet to see this being a problem or a source of a bug.

1

u/Vowek Sep 25 '20

If you work in project with many people these people will use different part of C++ and you need to understand these parts. Ergo In projects comprising of more than one person you need to know a lot more than you use.

5

u/Hacnar Sep 25 '20

I've been working on a project with over 1 million LoC in C++, with several other people, for over 5 years. Anytime you work with multiple people, you need to understand their code. That doesn't mean you need to know the whole language, otherwise everyone would know the whole language they use.

3

u/serviscope_minor Sep 25 '20

I disagree. The various features in C++ are there to tackle various problems. If you don't have that sort of problem then you shouldn't be seeing that feature used.

There's also a difference between using code written with those features and writing those features. The codebase I'm working on makes fairly heavy use of Eigen in places. Few people are really up to speed enough on old fashioned style heavy template meta programming to understand the implementation of Eigen in detail. The numerics people certainly don't on the whole but they can write good quality numerics code to calculate complicated things. Many of the pure software engineers don't either (or are so rusty as to be the same), but we don't go digging into the Eigen code anyway, since it's well maintained externally and we're just not in the situation where we're fighting bugs in that library. Some of the senior people can, but there's not the need.

So you could say we're using lots of arcane template metaprogramming, and we are in one sense, but we're not writing that metaprogramming. And it would be a worse language if a library like Eigen could not exist.

2

u/serviscope_minor Sep 25 '20

C++ became more and more complex :(

Really? What about the current behaviour of a[b,c] is intuitive, and how does deprecating it make C++ more complex?

Likewise initialisers for structs, how is making the language more regular making it more complex? If anything removing special cases (lol emplace doesn't work with structs lol) makes it simpler not more complex.

2

u/pjmlp Sep 25 '20

C# 1.0 vs C# 9.0, just as one example.