Hello and welcome to C++ weekly. I'm your host Jason Turner. I am available for on-site training
contracting and code reviews so C++ 17 was completed and officially made a standard in
2017 and
now the committee has started working on C++ 20 features, and there's been a few things that have been approved but
Obviously we're not seeing widespread compiler support for these things yet
but we do have the opportunity to play with some C++ 20 features and
Well, I have the chance to do that so I'm going to do it, so we've got the clang
trunk build that's currently available on compiler Explorer and
I have enabled -std=c++=2a to a so now we can start playing with C++
20 features and now they're saying 2a because of course, there's no guarantee that will actually see a version of C++ in
2020 but committee has a good track record c++11
Was late c++14 and c++17 came out on time so we expect C++
20 will probably come out on time so the first feature that I'm going to demonstrate is
Initialization of bit fields so this is kind of fascinating to me if we've got our
struct S
And you would be forgiven if you had never used a bit field before
But if we wanted to say we've got this bit
This int i and it takes 3 bits and int j takes
5 bits then
We might expect that the size of our struct S is
Going to be 1, 1 byte, and let's see if that's the case no it is 4 bytes
That is because we are getting a padding of size s with 3 bytes to alignment
Boundary so this is padding. That's going to be required for this struct, but it's getting three bytes of padding
Which means that we are actually only using one byte, so if we increase the size of this we expect to see our
Padding change, and now it is reporting
23 bits of padding so we have our struct that is effectively 1 byte and
It contains these 2
bit fields now before
C++20 if we wanted to initialize these to some default value
We would have to do it
like this and
You know I don't see anything wrong with us necessarily, but this doesn't really play well with our
C++11 in class initializers, and we wanted to be able to do something like this and
now we can we can specify the default member initializers for width fields and
say that we want these values to be equal to 0 and effectively the compiler is going to synthesize a
constructor for us that does what we want it to do and
Now we can see the new warnings that we're going to get with our clang in C++ to a mode
Which we have this new warning that is warning this thing is not compatible c++98
c++11, c++14, c++17 and
We can't indeed see that if we take it back to C++17 standards, then we're going to get this
Bit field is a C++ 2a extension. It is still allowing us to do it so in this case
This is the kind of thing that you want to make sure like you're pedantic warnings are enabled for a so you're not
accidentally using features that are outside of
The C++ specific standards that you've got now obviously we're getting these warnings because of -WEverything anyhow and
We probably would even without pedantic with this particular example
There you have it so clang is good about warning about this in any case
but just be sure to take advantage of your warnings and
This is the first feature that we're able to demonstrate on C++ weekly for C++
20 or 2a as it is currently being called by your compiler vendors
Be sure to subscribe follow me on Twitter and check out any of the links below
