CppCast - Move Semantics
Episode Date: June 4, 2020Rob and Jason are joined by author and ISO committee member Nico Josuttis. They first discuss a blog post on faster integer parsing and CppCon registration opening up. Then they talk to Nico about the... book he is currently finishing on C++ Move Semantics and other books he's written in the past. They also discuss proposals that Nico has worked on for the ISO committee. News Black Lives Matter ACLU 15 Tech Organizations Actively Supporting Black Coders Black Tech for Black Lives Faster integer parsing Bjarna AMA CppCon Registration Open Links C++ Move Semantics - The Complete Guide CppCon 2019: Nicolai Josuttis "When C++ Style Guides Contradict" p0660 - Stop Tokens and a Joining Thread The C++ Standard Library C++ Templates - The Complete Guide C++17 - The Complete Guide Sponsors Clang Power Tools Use code JetBrainsForCppCast during checkout at JetBrains.com for a 25% discount
Transcript
Discussion (0)
Episode 250 of CppCast with guest Nico Jusutis, recorded June 4th, 2020.
This episode of CppCast is sponsored by Clang Power Tools, the open-source Visual Studio extension on a mission to bring Clang LLVM magic to C++ developers on Windows.
Increase your productivity and automatically modernize your code now.
Get ahead of bugs by using LLVM static analyzers and CppCore guidelines checks from the comfort of your IDE. Start today at clangpowertools.com. And by JetBrains, the maker of smart IDEs and tools
like IntelliJ, PyCharm, and ReSharper. To help you become a C++ guru, they've got CLion,
an intelligent IDE, and ReSharper C++, a smart extension from Visual Studio. Exclusively for
CppCast, JetBrains is offering a 25% discount
on yearly individual licenses on both of these C++ tools, which applies to new purchases and
renewals alike. Use the coupon code jetbrains for CppCast during checkout at jetbrains.com
to take advantage of this deal. In this episode, we discuss integer parsing and conference news.
Then we talk to author and ISO committee member, Nico Jusutis.
Nico talks to us about his upcoming
book on move semantics and much more. Welcome to episode 250 of CppCast, the first podcast for C++ developers by C++ developers.
I am your host, Rob Irving, joined by my co-host, Jason Turner.
Jason, how are you doing today?
I'm okay, Rob. Yeah.
There's a lot of craziness going on in the world today.
Yeah, yeah.
I did want to briefly comment on that.
Obviously, we still are in pandemic mode with COVID going on.
And on top of that, we have a lot of protesting going on in the U.S. right now.
I did want to just briefly say I'm definitely in support of the Black Lives Matter movement.
I know we don't ever comment on politics on the show,
but I don't think this is really a political issue.
It's kind of a human rights issue, in my opinion, at least. So yeah, I don't know too much I'll say about that. But, you know,
one thing I've thought about is, you know, if you're in the US and, you know, you're listening
to this show, you're probably doing okay right now as regards to job stability and everything
with the COVID pandemic going on.
We've all been given checks from the government.
And if you're an engineer and you haven't lost your job,
you probably don't actually need that money too much.
And it's just extra money.
So maybe consider donating some of that to the ACLU.
There's bail funds to get protesters, uh, you know, who are peacefully protesting
out of jail.
It's also black charities, uh, consider maybe donating some of that money to some of that.
Sounds good.
And then just the last thing to bring it back to, to us, um, you know, we would certainly
love to have more guests on the podcast from the black people's host community.
Um, it's hard to find guests, unfortunately,
because we usually pull a lot of our guests from
who's giving conference talks and things like that.
And most of the black C++ developers that I'm aware of
that have given conference talks,
we've had most of them on the show already.
Yeah, right.
Yeah.
But if you're out there and you're listening and you have any ideas for a potential guest or would like to come on yourself, definitely reach out.
We are always looking to diversify the guests we have on the show as well.
Okay.
Well, at the top of our episode, I'd like to read a piece of feedback.
This week we got a tweet from Nigel saying,
CppCast just found your podcast and
working my way through your excellent back catalog. I feel part of the C++ community now.
Lockdown got a bit easier. Keep up the great work. And yeah, thanks for finding us. Thanks
for listening, Nigel. I'm just sad that he found that, you know, three months after lockdown
started, he could have, you know have been entertaining himself this whole time.
Yeah.
Yeah. Well,
he's probably still has a lot to go through and notice how much longer this
is going to go on.
Yeah.
And as I think you pointed out on Twitter,
you can look into C++ Weekly as well.
Yeah.
Yes.
There's a lot of content on my YouTube channel now as well.
Okay.
Well,
we'd love to hear your thoughts about the show.
You can always reach out to us on
Facebook, Twitter, or email us at
feedback.cvgas.com and don't forget to leave us
a review on iTunes or subscribe
on YouTube. Joining us today
is Nico Jositis
He's well known in the programming community
because he not only speaks and writes with
authority, being the co-author of the
worldwide bestsellers C++ Standard
Library, C++ Templates Complete Guide, C++ 17 Complete Guide
and C++ Move Semantics Complete Guide
but he's also an innovative presenter
having talked at various conferences and events
he's an ACT member of the C++ Standardization
Committee for more than 20 years
Nico, welcome to the show. Thank you guys
it's an honor to be there, thank you very much
and I should say I didn't get a check
from the US
Yeah, I guess that's only for US listeners Thank you very much. And I should say I didn't get a check from the U.S.
Yeah, I guess that's only for U.S. listeners.
Yeah.
But it's a good thing. Your advertisement of how to use the money, that's a good thing.
I fully support that.
It was a crazy week, yes.
And there are more important things than C++ definitely going on in the world right now.
Yes.
And I think I'll take just a second sorry to interrupt
your intro here Nico but
also to say at the moment our plan is
to keep doing c++
cppcast episodes
as planned on our regular schedule
there was media blackouts earlier
in the week but at the moment
we're still planning to release this on Friday
I think I think that was more of
a one-day protest
on Tuesday. Right.
Okay, well, Nico,
we got a couple news articles to
discuss, and then we'll start talking more
about your books and other things you've done
in C++, okay? Whatever you
want!
So this first one is
an article, Faster Integer Parsing,
and some interesting stuff going on.
He benchmarked different methods of integer parsing
and then started rolling his own solutions
and showed how performant you could make integer parsing,
although he admits he's completely not doing any input validation or anything like that.
Yeah, I'm like, in some of these cases with no validation at all,
assuming that there's going to be exactly 16 bytes of input is a little risky to me,
even if you know the source of your data.
I don't know. What do you think, Nico?
A couple of things.
So first of all, I have given up to understand how well compilers optimize.
I think that all happened when Matt Godbold gave a keynote at C++ on C,
where he explained how even for ordinary code iterating over vector of ints,
how compilers
vectorize that and using
SIMD, etc.
That was the last
moment I taught to everybody.
Well, forget about your thoughts.
Let the compilers do the work
and the experts who program
them. We shouldn't forget that because there are some
smart guys behind.
So, yes, there might be some cool things to optimize with certain and special requirements like here.
But the overall rule is first measure where your problem is
and then optimize.
But only where you measure that it's worth it.
And that's all.
Although I think I totally agree with you,
but also feel like since the benchmark here discusses,
what, four different options from the standard library,
like clearly your default should not be string stream
if you can use A2L or car con instead.
We know that.
We know that.
We know that for years now.
It's just somebody has to do the work to come up with something better.
I think a lot of the problem is the functions that rely on the current locale. That always adds overhead to look up the current locale and decide what kind of parsing to do. I just double-checked that.
CarConf does not use the local.
Yeah, that was a design principle of CharConf.
It's an explicitly designed principle.
So no localization and no memory overhead.
So memory is prepared and just the fastest thing to do.
So we wanted to replace a printf or sprintf with a percentd or so, and that was a major
goal because we saw that still
be used a lot, and we
see that still. And
yes, so we know that.
And CharConf is definitely
a step in the right direction, I think.
Yeah, okay.
I was there when we designed,
no, when we designed, when we standardized C++ in 98, I was there already.
And I know how, looking back, I think we made some mistakes.
And the biggest mistake is if you have a nail, you always use a hammer because it works.
And so we started to come up with some abstractions.
We didn't see the price.
And I think now with the knowledge of 20, 25 more years,
we can do a lot better with redesigning a new string and stream libraries.
And we have a couple of ideas.
We even had discussed a few ideas.
But as I said, somebody has to do the work.
This is a committee-driven thing.
Somebody in the committee has to stand up, come up with an idea, discuss it, drive it through, et cetera.
That's a problem.
So I think it makes sense personally to do a little gist of a sidetrack here at the moment.
Since you've mentioned the committee several times,
let's see, a committee meeting is going right now, basically, right? I mean, theoretically, you would have been in Varna. So since you have been an active member of the committee for the
last 20 some odd years, what are you doing right now? What are you doing right now on the committee? Like, how is this working for you at the moment?
Actually, we discuss it.
So the problem is we discuss how far we can go without face-to-face meetings.
Because face-to-face work is a lot more effective than anything else. And we currently in the background have a lot of telcos and telecoms and
offline discussions. The problem is now I could spend the full time just visiting the different
meetings we have now scheduled online. And that needs some full-time availability.
Why the three weeks we usually have in a year allow us just to concentrate on these three weeks.
If you are not a professional, just doing CPath all the time, it's already a big problem to catch up all the different new things that come up and the discussions around that.
And it's getting worse right now.
But we are discussing just that we postpone all the big new things,
not just bug fixing and small evolutions, to delay them until we can meet again.
And that's an ongoing discussion.
That's interesting.
So it's potentially taking much more of your time right now if you were involved in all of the telecoms.
If I don't have the time.
Right, if you did.
So I will end up with the problem I have a couple of times already in my life
when we were close to a new standard or even the new standard was out and i tried to document it
as one of the the authors of c++ and i suddenly understand some things i don't like i understand
things i don't understand them and i have to catch up and uh so um it will be interesting
but anyway c++ is already big enough enough, so I have enough to do.
So with all that in mind,
now I'm curious, if someone
who's listening thinks,
well, I have a bunch of free time
right now, maybe they've been furloughed from their job or something,
and they are interested in becoming
involved in the committee, is now a
good time or a bad time to try to become
involved?
You can spend the rest of the hour about that.
Well, in general, everybody helps.
Okay.
And the problem is everybody might create new work.
Right.
And raise the workload.
If you compare what we do right now with how we worked 25 years ago, it's incredible.
I mean, we didn't have laptops.
We didn't have the internet when we standardized the first CBUS bus stand-up.
So we were standardizing on paper.
People were making their proposals on paper, and we were discussing the papers in the room.
So now it's a lot faster.
We have over 200 people now who discuss in subgroups.
So the good thing is if somebody comes in and has a new idea, we have an earlier new idea.
If somebody comes in and finds a bug, we earlier find a new bug. But the more people come in,
also the more widely spread the ideas, the interests, and the styles we use in C++.
And that also creates additional burden to the standardization.
And we currently face a problem that we really have topics where we think the workload is just too high.
We have to reject that.
Although people have worked on the thing for a couple of years.
So that's a bad situation.
So it's really mixed feelings right now.
So, but come, come and join.
Best thing is review, help us in the small parts
because a lot of people like to propose cool new features,
but doing the work, the dirty work,
implement them, check them out,
test suites before we standardize
and ship out standards.
This is something where we all the time
can need more help and more support.
So that's interesting.
So basically anyone could go to the mailings.
In every episode, remember, there's a new mailing out. We talk about that here. Anyone could go to the mailings, read whatever papers they wanted to, and then gives feedback. And by the way, we have changed
our
schedule for
new papers. Now
not having on-site
meetings until the end of the year.
Everything is cancelled by ISO.
So we
have monthly
new documents and proposals
for the standard.
In the past, we only had a collection of documents before and after each meeting.
So we have changed our document schedule already.
Wow.
You should have a meeting or discussion every month now about the new papers.
That's great.
Okay.
Next brief article we have is a list of Ask Me Anything live Q&As.
And if you're listening to this on the day it comes out, June 5th,
then you could tune in for a chance to ask Bjorn Strusup some uh q a questions in an ama uh his is going to be
i think it's 12 30 at uh on june 5th and what time zone time zone yeah gmt minus 70 pacific
oh these are all in pacific time okay good so 12 o'clock pacific time that's interesting because
this is a London-based
group. I am so confused because I had just been assuming this was in London time zone this whole
time. So it looks like they want you to register, by the way, but it is free, but registration
closes tomorrow. I thought it was June 5th. Right. The registration closes tomorrow, but the actual
AMA is going to be June 17th. Okay. Right. So just for the record, this is a whole programming languages conference that is going to be fully online.
It's like five days of conference.
So I might sign up for this because there are interesting things here.
Have you ever been to any of the ACM or SIG plan meetings, Nico?
I don't think so.
Maybe Uppsala was driven by... I don't know think so. Maybe Uppsala was driven by...
I don't know who.
Yeah, Uppsala is run by...
Uppsala was one of the leading OO conferences
in these times when this was mainstream.
Yeah.
I'm not sure, no.
I can't say anything completely about that.
Yeah, I think these are mostly
like programming language theory kind
of conferences,
but it still looks like some of these things look
like fun to me.
And then the last thing we have to discuss
is an announcement from
CBPCon. Registration
is now open, and there's
a long post here about how
they're definitely still
paying attention to the situation with COVID
and that the venue has some medical experts that they're working with to make sure that they run a safe conference.
And it sounds like it probably will be a smaller attendance,
that they're expecting a smaller attendance than they have than they had
last year as we peak on um and that they're already planning on not having like some of the social
gatherings uh you know with every attendee in a big room it's just not a great idea yeah they say
this is the first year ever they expect to sell out because they never had limits on registration
before but they're going to be doing a strict limit on registrations, assuming the conference goes forward as a live conference.
Right.
And you're already planning to go as a trainer, right?
Yeah, I give a class, a two-day class, and that was scheduled already behind the scenes
a long time ago, and the earlier you book flights, the better.
Right.
However, now you don't know.
So I have booked already the flights, and I am wondering whether I will be there or not.
Although I think CppCon is really on the borderline of where even conferences are possible again.
So public events where you can have distance between the people should be possible then.
I think that the biggest problem is something like a football game or so.
That would be the biggest problem to come back with something or a concert or a rock concert or so.
So I'm 50-50.
I'm divided whether it will happen or not.
Let's see. So I can say I'm 50-50. I'm divided whether it will happen or not. Let's see.
So I can say I'm safe.
So when I fly there, I have no problems.
I had COVID-19.
So for those who think it's a fake thing, I can tell you it's not.
And it's not fun.
And it's not just the flu.
It was no hospital, but it was everything else.
So please take care and be careful.
It's not fun.
Do you fully recover?
Do you know how you got it?
Yeah, I know.
I know.
There was a hotspot in Europe in Ischgl for skiing, and I indirectly got it from there.
All people who were there more or less in that week got it, and that's a couple of people spreaded it over europe oh wow yeah that and that yeah that turned everything down so i i had it i was aware
already that i could have it so i was very careful being um in my at home at us we have two um
two floors so i was alone at the at the upper floor and um yeah and i think i i fully recovered
um so yeah you had a milder case of it and didn't need to go to the hospital well the
milder case if you don't know that you have it and which are interestingly um more women than men have. And so, yeah, so I think I'm in the middle,
but I'm in the age where it's already a risk,
a higher risk than for others.
I had two or three nights where I was close to go to a hospital,
but unfortunately we had, in Europe,
we have a good working health care system
everybody has health care insurance here
so that things might be a little bit better than in the US
without criticizing the US in general
but I think regarding that topic we are in a better mood or better situation than other countries.
Yeah.
Okay.
Well, let's start talking more about C++.
As we mentioned in your bio, you have written several books.
How many books have you published now?
Oh, that's tricky.
And it's not only C++.
I started with OSF Motif and the X Windows system if you have ever heard about that.
Oh, goodness. Oh, wow. That was
30 years ago and a company
where I joined.
So I got fixed.
I got a little bit
book writer junkie
guy.
So every time I got
a new child, I had some time
waiting for it to have the birth.
And then I started with a book, and then two years later, it was finished.
And then pretty early, C++ books.
And so I think I have something like six C++ books written, but also some others about service-oriented architecture and other topics.
The topics I had in my life, in my IT life,
I wrote about that.
But we can't infer from this that you've
had like 10 children
then.
I don't say anything as an answer
here.
It's more than two and less than ten.
And there will be no new ones.
Right.
Do you primarily write in German or English?
Are your books available in both?
Well, I started to write them in German.
So the first C++ books were two books in German,
just teaching C++ how to learn it
and about the C++ standard library,
which is called the C++ Standard Bibliotheque.
And I should say I've prepared it here.
D in Germany is spelled D-I-E.
D-I-C++.
Yes, that was the moment.
I took this book to my first meeting, to my first ISO meeting,
and then Jana came around the corner and saw this.
He said, interesting, and you delegate here from Germany, so what's your message here?
So this was my introduction to the communityianna gave me get made the connection so that these books
were um translated or republished by pearson and that was very successful so c++ standard library
is one of the of the major books um i have and but also for the company, it's a good thing. So, yeah, it was
a creative part of my career,
so I'm very thankful about this
opportunity. And then after that,
you, from time to time, write
new books. Yeah, that's enough
to tell about C++, unfortunately.
I'm 222 episodes into
C++ Weekly and wondering if there
will ever be a time when I run out of content.
I don't think so.
No.
There's no way.
No way to do it.
Because even if you go back to your first episodes, you will see, oh, I was wrong.
Yes.
Definitely.
Yeah.
It's embarrassing.
Embarrassing how late I sometimes learn things I should know for 10 or 20 years now.
And it's hopefully not only me.
We have a problem in the C++ community that nobody knows the language as a whole.
We have a lot of specialists for certain parts and they are good experts.
We have a problem with the generalists and especially we have a problem with
the generalist being on a level that can help beginners to understand the
language.
That's a problem now.
Too many experts.
They're incredible well,
but sometimes they lose a little bit of the ground.
And that's a problem. That's usually my part because I have to explain it to beginners.
I'm not such a smart guy.
I can explain and analyze and explain.
So my role is a little bit different.
I don't event.
I explain and I criticize about things I can't explain. So that's usually my role is a little bit different. I don't event. I explain and I criticize about things I can't explain.
So that's usually my role.
OK, so if you don't mind my asking, you just said you're surprised at the things that you thought you should have learned 10 years ago.
Is there something that recently came up that you are like, I can't believe I just learned this?
Oh, yes, of course. I mean, I just at Christmas time.
So what is it?
Six months ago, I decided, let's let's write a book about move semantics, because whenever I explain it, I see that I have some uncertainty and people are very surprised even if they know it for years now.
And this book has now 200 pages.
That's the one you're working on right now, right?
Yeah, it's Agile written, so I started there.
So it's published already in the pre-draft version.
And it will have 200 pages.
Right now it has 170 or so.
And they are already published as a draft, so you can download it.
And, yeah, I learned a couple of things about semantics I didn't know.
And we have that same experience now in the C++ committee.
We are now discussing what does it mean to have a valid but unspecified state.
And we have very different opinions on that.
So it seems we come back after nine years of existence to some basic questions about move semantics,
how it is and how we should deal with it.
And that means something.
Yeah.
And you said the book's currently 200 pages.
How much longer do you expect it to get?
Well, as I said, it currently has 150, 170 pages.
Oh, sorry, I misunderstood.
But I expect something like 200.
So it's a few chapters are only missing.
So two of them, I think, no except.
And perfect returning, people always ask and learn about perfect forwarding,
but the rules for perfect returning are even more complicated.
And you come to a DECO type auto, for example.
And the application and the special things we have in the standard library
about how move semantic is supported by the different types.
So 30, 50 more pages, so something like 200 pages will be there.
And that's enough.
That's far too much.
When do you think the final version of the book will be ready?
Hopefully soon.
And thanks to COVID-19, it will be soon.
So because a couple of on-site trainings are cancelled
so I have more time than
I planned
so
I would like to finish it
I thought I'd do it now but
I come just from three weeks of online training
so online training is
a reality now
but yet
now in the summer there should be some time to finish it.
And I'm still discussing with, I don't know, Sean, Parent, Howard Hinnant, Herb Sutter, and others, the details of some semantic meanings of move semantic.
So let's see. Yes, we've, I think, at times discussed the arguments things are missing. And we are currently, we had
a discussion about exactly that term in the last standout meeting, which was in February.
And some tasks that some people in the committee, I think it's Jonathan Wakely, should come up with
a replacement of the wording about valid and unspecified state. So we are
currently working on the topic.
However, we still have
different opinions. So let's
see. So I'll
have to update my training material
at some point here probably then.
Yeah, or
just refer to my book.
Of course.
Tell my students. And if you have more questions, just read these 200 pages.
No, I mean, it's interesting.
I know my slides.
I change my slides a lot while writing this book. where I was not aware of, is the state you can bring in your objects, which is an invalid state,
just by using the default behavior of move semantics. So you can implement a class pre-C++11,
where you think all your invariants apply to all the objects you have from this type. And suddenly, in C++11, you can get a situation,
a state where these invariants don't hold anymore for moved from objects.
So for objects where the content has been moved away.
But in general, we say, well, if the content is moved away,
the object should still be a valid object.
So there's some discussion what this exactly means, but it's definitely more than just
you can destruct it. The usual meaning is you can somehow assign something to it,
whatever that means. We have a lot of ways to assign a new value to a vector. For example,
calling clear on a vector is an assignment of a new value. And the interesting thing is if you
do certain tricks in your class, like having, for example, a string member and an int member,
both just representing the same value, and your invariant is that this is a consistent state of your object.
Your default move operations will move away the string value,
but not the int value.
So you create an inconsistent state.
And that might be a problem.
Might not, but at least it opens some discussion
about how to deal with this object.
And, yeah, so that was one topic I learned about, and I got some interesting examples by others, by Tony Fonerat and Howard.
And, yeah, I'm curious what the style guides are from that, because my goal is always to explain this to application programmers
so what are the consequences for you if you implement a class what should you do
and the funny thing is if you then just want to disable move semantics in your class say i want
to have the behavior of before c++ 11 just the behavior as when i designed and implemented the
class you have to violate the rule of five,
because the rule of five can't solve your problem unless you implement all the operations.
In fact, you have to default the copy constructor. You can't delete the move constructor. You have
to default the copy constructor, but do nothing with the move constructor, which is a violation
of the rule of five that says either you declare one of
them and none of them or all of them.
Right.
Okay.
I'm sorry.
I was just thinking through the implications here.
Yeah.
Yeah.
So these are discussions I have.
And then I discussed this with Howard.
And Howard is telling me, yeah, the rule of five is anyway wrong.
And Howard, forgive me.
That might be an oversimplification of your statement.
But there's some truth behind.
So we are questioning, again, style guides, recommendations.
And at the end, I have to explain this to ordinary programmers.
So I have to tell them, when do we have to be aware that your class might create objects in a state you didn't expect that this will exist?
And that's where I'm currently working on.
So for the sake of our listeners, I just want to take a step back on this, why you have to default the copy constructor but not delete the move constructor.
Yeah. default the copy constructor but not delete the move constructor yeah so if you default the copy constructor then that prevents the compiler from generating a move constructor for you
so anything that looks like a move actually becomes a copy yes but if you delete the move
constructor then you're going to break code that expects a movable type no you break code that expects to copy. Because if you delete the move constructor, you say, whenever I call move construction, I will not use a fallback mechanism to come back to the copy constructor.
And that means you can't even initialize or assign a temporary return value to your object anymore.
So that's not code that explicitly uses move.
It's just where move could be used.
So you break all cases where move could be used,
but a fallback is possible.
And in the past, only the fallbacks using copying were possible.
But with the delete, you disable this mechanism.
And therefore, delete disable this mechanism. And therefore, delete breaks this
behavior. So the only other
solution would be that you implement
the move constructor doing the
same as the copy constructor, which is a
little bit crazy.
I even tried out that the
move constructor could call the copy
constructor. That is possible.
But no, don't do that.
So the only option from that is break the rule of five here.
Don't use it.
Huh.
Okay.
You learned something.
Yeah.
It's interesting.
I want to interrupt the discussion for just a moment to bring you a word from our sponsors.
Clang Power Tools is the open source Visual Studio extension on a mission to bring LVM tools like Clang++, ClangTidy, and ClangFormat to C++ developers on Windows.
Increase your productivity and modernize your C++ code with automatic code transformations powered by ClangTidy.
Find subtle latent bugs by using LVM Staticatic Analyzer and CBP Core Guidelines checks.
Don't fiddle with command line switches or learn hundreds of compiler flags.
Clang Power Tools comes with a powerful user interface to help you manage the complexity directly from the comfort of your IDE.
Never miss a potential issue by integrating Clang Power Tools into your workflows with a configurable PowerShell script for CI-CD automation.
Start experimenting on your code today.
Check it out at clangpowertools.com.
So you've talked a bit about how you see your role in the ISO committee as kind of being an explainer and looking out for kind of everyday application developers. Do you have any,
are there any specific proposals you worked on, you know, that were aimed at trying to help everyday application developers?
Yes.
I'm just curious.
I fixed std thread
for C++ thread.
J thread was your work then?
J thread was my work.
Because we made a huge
mistake. We knew that we were doing that.
We had to vote.
I'm still a little bit angry about that.
We had to vote that we want to fix this,
and we didn't.
And so the problem with StoodThread
is that it's not an RAII type.
So whenever you run into an exception
or other situation,
you first have to call join or detach
before you
leave the things. And I always ask to fix that with support of others like Herb.
And they always rejected it and say, no, too much work. That's not what we want, etc., etc.
And then finally, I think with the third approach, I was successful. And the funny thing is a few guys now are using it already who really were against it because they thought it's not worth the effort.
But now we have a type which you can just declare and it works the usual and a safe way.
And you have an interruptible thread or at least a thread object where you can signal interrupts, which is also a pretty common thing.
And it's one reason a lot of people don't switch from boost thread to stood thread.
And so, yeah, I did that.
And I'm not an expert on concurrency.
I only documented for trainings and for the books.
So I learned a couple of things.
A couple of experts were involved into example implementations, and we still found a lot of bugs with some first implementation.
So looking back, just signaling interruption in general for a threat in a threat-safe state is a huge, complicated problem.
I want to talk about that.
And I'm pretty happy that we have this mechanism now so that people don't have to invent it on their own
because they will make several mistakes.
Concurrency is a nightmare in every sense.
So, yeah, so JThread was one example where I fixed the standard.
I think the other part was atomic int, that if you have an atomic int and using brace initialization, it still has undefined behavior, undefined value.
So that was the only place where brace initialization didn't initialize.
I also fixed that.
I didn't initialize, I also fixed that. I didn't fix it. I drove it through the committee and said this is fixed because I hate to teach this
and to say, well, in general, race initialization initializes except for atomics.
And due to some future planned compatibility to C, which never came.
So that's a bad background of the story.
And there are other topics
like, I don't know,
my count work is to
fix the range-based for loop so
that it does not
delete temporaries you have on the right
side in the expression behind the column
if you have a nested expression.
Isn't that the point of
the new init on the left side?
The new init? The new init on the left side? The new init?
The C++24 init.
The new init on the left side you can use as a workaround.
That's right.
But you have, I would say, the range-based follow-up is the control structure now to teach to programmers about C++.
And what I have to teach now is the thing on the right behind the
colon if this is an expression returning a temporary object or what we call a pr value
and then using a getter returning a reference there then it doesn't work so explain this to
beginners a couple of words they don't understand and what what they learn from that is they should only use names behind the column.
That's fine.
That's something I would propose for safety critical standards like MISRA also,
just as a rule of thumb.
Other companies warn about the range-based for loop now,
and we should fix that, and we can fix that. yes, and we have to discuss and find out the price.
But it seems that I'm the only one who's really outraged of this problem.
So things like that drive me crazy because I have to teach it,
and I can't easily teach and say with the range-based for loop,
you can easily iterate over range,
even if it's returned by an expression from somewhere else.
So do you have a paper in the works then for this?
It's at work. I think the first version is not out yet.
If you send me an email, I'm happy to tell you about it.
It will be discussed pretty soon. Behind the scenes, we need some preparing discussions so that we are not voted down immediately.
Yeah. I'm curious, if you don't mind my asking right now, if your approach is to address the range-based for loop itself, or if your approach is to address the lifetime extension rules so that the lifetime of the thing being returned is extended in that case.
That is exactly one of the discussions we have right now.
Okay.
I'm pretty, a lot of people say, so then let's do it the full time.
But Bjarne, for example, says we had that as a proposal in the past in C++ and and we failed, and we got a lot of problems. Because if temporary objects return big matrices,
their lifetime will be extended and your footprint raises.
And so there are good reasons not to extend this in general,
especially as it applies to a lot of code.
So if you only say it, it will be in the range-based follow-up,
because that's the only place where you don't see a semicolon, but which is behind the scenes.
So I can say everything, it looks more like a function call parameter and everything will be valid.
You only have the problem or you only have the fix in code that uses more complicated expressions in the range-based follow-up.
And that reduces the risk for existing code in other places. in code that uses more complicated expressions than the range-based for loop,
and that reduces the risk for existing code in other places.
And we always have to be careful not to break existing code,
even not regarding performance.
So I'm strongly in favor of not fixing this in general,
just fixing the description of the range-based for loop so that the current description is not used anymore.
Okay.
Yeah, I would be curious to see what your proposed looks like.
Like, yeah.
It's easy.
So for the expression behind the color, every temporary U return, even in sub-expression,
the lifetime is extended until the end of the loop.
So it's as if the thing behind the colon
is passed as a full expression as a function argument.
Right.
And then also everything is available until the function returns.
The part that, well, we don't need to spend the rest of the interview discussing this.
The part that I'm having a difficulty visualizing is the standard says, okay, in a range-based for loop, it gets expanded to this.
And it shows, like, okay, it's the variables here.
That's the problem.
That's the fundamental problem.
Our problem is that all the people who argue about it say, but this is what the range-based for loop is.
No.
The range-based for loop, that's a black box.
We don't have to describe it that way.
We can say, no, it's slightly different.
And so I first want to clarify what the rules are as a black box,
and then we can think about how do we have to fix the description
so that this fix is formally documented in the standard. There is a phrase in a standard.
There is a phrase in the standard.
What is it?
I can't remember what it is now.
But I could see a solution where you document that each sub-expression is itself assigned
to a temporary R-value reference, and then it would just work.
Yeah, that's roughly the idea.
Oh, okay. All right, very good.
And then, thank you.
I'm not a compiler implementer.
And I currently have it implemented already to see what is the overhead you create from that, etc.
So, yeah, but that's roughly the idea.
It should only be compile time overhead. The rest of it should be transparent.
No, no, no, no, no.
No, no, you have runtime overhead.
You hold references longer.
It's a little bit – what I measured was almost no runtime overhead,
but the footprint because you keep temporary objects longer alive,
which might mean if you have an expensive object it lives
throughout the whole loop although before it didn't live throughout the loop but before it
was broken anyhow right and no but in the good case so if you return it by value and then use a
use an access function that does not return it by, that case would now become worse.
Okay.
Because you can only see that I return by reference something.
You don't know where it comes from.
It might not be a getter.
It might return by reference a static value or so.
So, yes, a broken case is fixed.
That's fine.
But the good case is where we have sub-expressions what might hold return values longer than before.
And that would have some consequences.
So you might end up with two or three large
temporaries on your stack instead of
just the last one that you cared
about. Yes, exactly.
And the question is, how big
is this danger? How many
code would we break?
And is there a workaround?
Yes, we could then say, well, you see new range space followed with initialization.
And so, but these are the discussions we have to do because a lot of people are very, very careful with changing existing behavior.
Right.
And that's a good thing.
That's a good thing. Right. have now it's a nightmare it's simply a nightmare okay these are my fights only complain i've never went something only complain
okay uh bringing it back to your your books a little bit um which of your books have been
the most successful and and which uh would you, I guess, most recommend to our listeners?
All of them,
right?
Well, according to the numbers,
it's clearly the C++
Standard Library, which became
the book, the
worldwide known and
translated book about the C++ Standard
Library. And it has, I don't know,
something like 75,000 copies sold in the world in, I don't know, something like 75,000 copies sold
in the world in, I don't know,
10 or more languages.
Wow.
On the other hand,
we get a lot of good feedback about
C++ templates, the complete guide,
which is, I'm only a co-author.
The experts were others,
were David Funderford
and Doug Regger.
My part was to make it
readable for people like me.
So that I
and even the ordinary application
programmer doesn't come in this
situation like
Alessandro Alexandrescu's
book about templates where
you're easily lost after
the first 10 pages as a known expert.
And yeah, and I think these are the major ones.
And the rest is now, C++ is so complex.
I can't update all these books every three years.
So C++ 17, complete description of the new things.
The same will come with C++20.
I'm curious how many pages this will have.
And then as a side note, I care about C++ move semantics,
so everything has its place.
Do you think you're definitely going to work on a C++20 guide after move semantics?
Yeah.
So like the C++17 book, I have a complete description of all the features, both language and library,
but not as an early seller as I did with the C++17 book. Plus, I want to publish it when I can try out all the new things.
So with some experience and implementation experience,
and maybe I go to CppCon and hear talk from Jason about how to use the,
I think it was the value types and your value types at two years ago about
variant and optional and any.
No, that wasn't me.
And at a certain project so that I can collect first real experience
and seeing the different consequences in the different compilers
so that I can, besides describing the features,
also give some first feedback how to use them,
where to be, I don't know the English phrase,
so where to be careful about, and et cetera.
And that's also planned for C++20, definitely.
So I will know until I'm dying.
Every three years, I will write a book.
That should keep me alive.
I am curious, with at at 200 pages this deep dive into
move semantics who is the target audience for this book everybody who doesn't know everything
i mean um the book has um if you're a beginner and want to understand move semantics um it might
be enough to read the first three chapters.
But it's really hard to explain move semantics, even for beginners.
And I see the uncertainty in projects where people are surprised what they learn when I teach move semantics.
I'm pretty sure you have the same experience. And so this is one of the features where people have a lot of half or quarterly knowledge
and they're using it, but they don't know exactly what they use.
They don't know exactly how it works and what is possible and what the consequences are
even for simple classes like using reference qualifiers.
They don't know that.
And so it's everybody who wants to get a deeper understanding in one of the key features.
It's like the templates book.
Who's reading the templates book?
Those who want to write their first template and those who want to understand the details of Sphinx, et cetera.
Maybe the only people who should not read it is those who implement
standard libraries or other
deep diving frameworks. But even they
learn sometimes. I would imagine, yeah. It was funny this year I gave
two trainings in companies where part of the audience
were people joining the C++ standard committee.
And they were surprised in a couple of places.
So that's our problem.
As I told you before, they had even proposed new features, but they didn't know details of other features.
And, of course, I also don't know all the features, but I'm getting more and more generalist because the best way to learn something is to try to explain it to others.
Right.
And besides having real experience.
And so I can now teach a couple of things where almost everybody is surprised, which doesn't mean I'm a good expert. I'm not. But I have a long time of knowledge, and I can present it.
That's all.
So everybody should buy all my books.
Okay.
On that note, I think that's a great place to end the interview.
Definitely go check out some of Nico's books.
It's been great having you on the show today.
Thanks for coming on, Nico.
Take it easy.
Thank you.
Thanks so much for listening in as we chat about C++.
We'd love to hear what you think of the podcast.
Please let us know if we're discussing
the stuff you're interested in, or if you have
a suggestion for a topic, we'd love to hear about that too.
You can email all your thoughts
to feedback at cppcast.com. We'd also appreciate if you can like CppCast on Facebook and follow
CppCast on Twitter. You can also follow me at Rob W. Irving and Jason at Lefticus on Twitter.
We'd also like to thank all our patrons who help support the show through Patreon.
If you'd like to support us on Patreon, you can do so at patreon.com slash cppcast. And of course, you can find all that info and the show notes on the podcast website at cppcast.com.
Theme music for this episode was provided by podcastthemes.com.