STL11: Magic && Secrets

Sign in to queue

Description

The C++ Standard Library expanded and evolved massively between C++98/03 and C++11.  It's easy to forget the magnitude of these changes, because they happened gradually and sometimes invisibly.  Some things (like shared_ptr, regex, and function) were developed in Boost in the early 2000s, before making their way into TR1 in 2005 and then C++11.  Other things, like container move semantics, automatically improve programs without human intervention.  Sometimes I can hardly believe that programmers used to live without non-intrusive deterministically reference-counted smart pointers!  We are fortunate to live in such an advanced and enlightened age.

In this presentation, I'll explore how some of the C++11 Standard Library's magic works, including how the Standardization Committee fixed pair's constructors (I bet you think that pair is the simplest type in the world - ha! wrong!) and how I saved a million zillion bytes of memory across all the programs using VC10+'s make_shared<T>().

Oh, and I will also reveal a secret that has never been announced before.

Tags:

C++11, C++

Day:

1

Level:

400

Embed

Download

Download this episode

The Discussion

  • User profile image
    gotcha13

    how do i watch this session on-demand?

  • User profile image
    vijaya1909

    Because of my office, I could not watch yesterday's event. I would appreciate if you share the link to watch this event. Thank you !

  • User profile image
    Charles

    , gotcha13 wrote

    how do i watch this session on-demand?

    Press play Smiley

    C

  • User profile image
    NotFredSafe

    At 35:12, you start talking about move semantics in the context of the flip function taking a string by value, but there are no move semantics involved here. Since the arguments are const char arrays, the string variable s is simply initialized by the constructor string(const char*), as it always has been.

  • User profile image
    STL

    I was taking shortcuts - move semantics would have been involved if the sources were str + "stuff" - but the point still holds.

    Compare:

    flip(const string& s) { string ret(s); // #1, reverse and return ret

    versus:

    flip(string s) { // #2, reverse and return s

    Given flip("meow"), #1 creates a temporary string, binds the const reference s to it, then (unnecessarily!) copies it into ret. #2 constructs s from "meow", as you mentioned, then flips and move-returns it.

    So, #2 made sense even in C++98/03, and it plays nice with move semantics.

  • User profile image
    c9xeo

    Great talk, it's always a joy to hear you talking! I hope you'll have a little bit more time for your Advanced STL series once VS11 is out.

    Then, any particular reason that nearly every slide was titled with a TVTrope? Smiley

    (Also, can we get access to these slides?)

  • User profile image
    someone

    What are cars, putters and stoods??!

  • User profile image
    C64

    , STL wrote

    Compare:

    flip(const string& s) { string ret(s); // #1, reverse and return ret

    versus:

    flip(string s) { // #2, reverse and return s

    Given flip("meow"), #1 creates a temporary string, binds the const reference s to it, then (unnecessarily!) copies it into ret. #2 constructs s from "meow", as you mentioned, then flips and move-returns it.

    So, #2 made sense even in C++98/03, and it plays nice with move semantics.

    So, when we have a C++ class X and we want to modify a copy of it inside a function/method, does it make sense (also in C++98/03) to always use signature like your #2? i.e.

    DoSomething( X x )

    instead of the more usual DoSomething(const X &) ?

    Thanks.

     

  • User profile image
    Dan

    Audio, at least on the "High Quality MP4", is out of sync with the video. Either that or STL can add ventriloquism to his already impressive list of skills and credentials ;-)

    Haven't downloaded the other qualities, maybe it's just the HQ MP4. FWIW, I downloaded the other videos, this seems to be the only one that's buggered.

    Thanks for making the videos & slides for the talks available, much appreciated. The only problem is that now I regret even more not being able to attend :-(

  • User profile image
    Dan

    By "downloaded the other videos", I meant the other talks, sorry if that wasn't clear...

  • User profile image
    Charles

    By "downloaded the other videos", I meant the other talks, sorry if that wasn't clear...

    Thank you, Dan. I noticed that too (when the file was intitially released). It was re-encoded to hopefully fix the HQ MP4 audio sync issue. Looks like it didn't work Sad

    We'll figure it out.

    C

  • User profile image
    STL

    c9xeo> Great talk, it's always a joy to hear you talking!

    Thanks!

    c9xeo> I hope you'll have a little bit more time for your Advanced STL series once VS11 is out.

    Me too. You can think of this as "Advanced STL, Part Infinity" for the time being. :->

    c9xeo> Then, any particular reason that nearly every slide was titled with a TVTrope?

    I came up with "Twenty Minutes Into The Future" and "The Reveal" first, and I liked them so much, I had to follow the pattern. :->

    c9xeo> (Also, can we get access to these slides?)

    The links are above: "Slides (view online)"

    someone> What are cars, putters and stoods??!

    I pronounce "char", "ptr", and "std" as "care" (it's the prefix of "character"), "putter", and "stood".

    C64> So, when we have a C++ class X and we want to modify a copy of it inside a function/method, does it make sense (also in C++98/03) to always use signature like your #2?

    Yes. It's sometimes better, and never worse. The advantage increases in C++11, but is present in C++98/03.

  • User profile image
    STL

    (never mind)

  • User profile image
    Jon

    As always i enjoy reading this awesome content, i hope you can find as much enjoyable content from me in return.

  • User profile image
    c9xeo

    > Me too. You can think of this as "Advanced STL, Part Infinity" for the time being. :->

    Heh, topic suggestion: Template meta programming! With things like Expression SFINAE, writing ones own type traits (like how to find out if a class has a certain member function), like you did in part 5 of Advanced STL.

    Maybe also about constexpr? But that isn't implemented in VS, right? Sad

    Hmm, what else... I'll think about it some more and report back. Smiley

    Edit: Oooh, another idea! I know this doesn't really count as STL, since they don't belong in that subpart of the library, but what about IOstreams? Especially the streambufs, locales, facets, codecvt stuff. As far as I know, there's only one real book on IOstreams, and it's from 2000 (IIRC). And personally, I think IOstreams are one of the more advanced topics, if you go beyond simple streaming to existing streams. Smiley

  • User profile image
    haohaolee

    Hi guys,

    I just wanna ask you guys, what question did Bjarne Stroustrup ask during the last few minutes? I didn't hear it clearly 

  • User profile image
    STL

     

    c9xeo> Heh, topic suggestion: Template meta programming!
     
    I am definitely thinking I'll do a "Core C++" series, including TMP but also template argument deduction, overload resolution, name lookup, etc.
     
    > With things like Expression SFINAE
     
    That's not yet supported by VC in full generality, but Herb announced post-VC11 "out of band" releases, where new features will appear. 
     
    > Maybe also about constexpr? But that isn't implemented in VS, right?
     
    Correct. constexpr is a very, very big feature. 
     
    > what about IOstreams? 
     
    iostreams fills me with dread and loathing. Maintaining the stuff is just about all I can handle. Tongue Out
     
    haohaolee> I just wanna ask you guys, what question did Bjarne Stroustrup ask during the last few minutes?
     
    IIRC, whether I would define NULL to be nullptr. I said yes if I had a time machine, no otherwise. 

  • User profile image
    c9xeo

    > I am definitely thinking I'll do a "Core C++" series

    Yes, please!

    > iostreams fills me with dread and loathing. Maintaining the stuff is just about all I can handle.

    Then make it a "Bash the IOstreams" session? Smiley Tell us what's so horrible about them!

  • User profile image
    haohaolee

    @STL

    Thanks

  • User profile image
    Comet

    Here is the future sample code:

    string flip(string s) {   

    reverse(begin(s), end(s));

     return s;

    }

    vector<future<string>> v;

    v.push_back(async( [] {return flip(" ,gnol oS"); }));

    v.push_back(async( [] {return flip(" dlrow"); }));

    v.push_back(async( [] {return flip("\n.ti wonk ew sa"); }));

    for(auto& e : v) { cout << e.get(); }

     

    Thanks,

    Comet

    purr more, hiss less

  • User profile image
    Charles

    , c9xeo wrote

    > I am definitely thinking I'll do a "Core C++" series

    Yes, please!

    > iostreams fills me with dread and loathing. Maintaining the stuff is just about all I can handle.

    Then make it a "Bash the IOstreams" session? Smiley Tell us what's so horrible about them!

    The Core C++ series has begun: https://channel9.msdn.com/Shows/Going+Deep/Stephan-T-Lavavej-Core-C-1-of-n

    STL rocks!
    C

  • User profile image
    lili

    What is matter your eyes?

Add Your 2 Cents