C++17 In Detail

04 March 2019

C++17 In Detail is 100% Ready!

C++17 In Detail

I released “C++17 In Detail” in August 2018, and I set the status to 90%. I didn’t expect that writing of that remaining 10% would take me so long :) Now it’s high time to set the counter to 100%.

That’s why I’m pleased to announce that my book “C++17 In Detail” is now done!

See the recent changes and a new code sample!

New Content

In February I pushed two smaller updates. I focused mainly on fixes and smaller sections:

  • Better explanation in the Structured Binding section
  • The improved part about code simplification with if constexpr (SFINAE and tag dispatching examples)
  • New sections in “Other Changes In The Library”:
    • std::scoped_lock
    • deprecation of std::iterator
    • polymorphic allocator and the pmr namespace (with new code sample)
  • Style and grammar across the second the whole book (thanks to Konrad Jaśkowiec!)
  • Other smaller fixes

The plan was to have a book with 250 pages… but the final version has 318. I think you should be pleased with those extra 68 pages of C++17 content :)

Here’s the link to the book:


C++17 In Detail @Leanpub

Example - Polymorphic Allocator, pmr::memory_resource

In the recent book update, I have a section about std::pmr. This is a new namespace that contains types related to the polymorphic allocator.

pmr is a huge topic, but in short a polymorphic allocator conforms to the rules of a regular allocator from the Standard Library, but it uses memory_resource to manage the memory. The allocator contains a pointer to a memory resource object, and that’s why the compiler can use a virtual method dispatch. With that capabilities, you can change the memory resource at runtime!

To illustrate some really basic things from pmr I came up with the following example:

#include <iostream>
#include <memory_resource>
#include <vector>

int main() {
    char buffer[64] = {};
    std::fill_n(std::begin(buffer), std::size(buffer)-1, '_');
    std::cout << buffer << '\n';

    std::pmr::monotonic_buffer_resource pool{
        std::data(buffer), std::size(buffer)
    };

    std::pmr::vector<char> vec{&pool};    
    for (char ch='a'; ch <= 'z'; ++ch)
        vec.push_back(ch);

    std::cout << buffer << '\n';
}

Briefly: we create an array on the stack, and then we give that memory to std::pmr::vector. This vector is a regular std::vector, but it’s predefined to use a polymorphic allocator. Since then the vector will allocate memory in the given memory chunk (thanks to monotonic_buffer_resource that manages the allocations).

monotonic_buffer_resource is a special purpose memory resource that allocates memory from the given buffer, but it doesn’t release it. So the memory usage only grows. There are other predefined resources like synchronized_pool_resource, unsynchronized_pool_resource or new_delete_resource.

I’m using char array so that we can easily print the “content” of the memory. This example can illustrate how the vector gets resized. Here’s a sample output (GCC):

_______________________________________________________________
aababcdabcdefghabcdefghijklmnopabcdefghijklmnopqrstuvwxyz______

Do you see the places where the vector “restarts”?

pmr opens a lot of new possibilities! For years writing a custom memory allocator for the standard containers was quite a painful process, but now it’s greatly simplified.

You can play with the above code @Wandbox
(Currently MSVC 2017 15.6 supports pmr and GCC Trunk (9.0*))

I plan to explore pmr and memory resources so that you can expect a few blog posts on that soon.

Until my content is ready, I highly recommend watching those talks:

Acknowledgements

With this update, my special thanks go to Konrad Jaśkowiec who was so kind and did a review of the whole book and caught lots of grammar and style things to improve :)

Book Mentions

So far the book was mentioned in several places.

Interactive Course

C++17 In Detail Educative

Thanks to the collaboration with the team @Educative we published C++17 in Detail as an interactive course!
You can see it… and even preview it for free here:
>> C++17 in Detail: A Deep Dive

It consists of 200 lessons, many quizzes, code snippets… and what’s best is that it has more than 120 playgrounds! That means you can compile and edit code sample directly in the browser… so there’s no need for you to switch back and forth to some compiler/IDE.
I think that such an approach increases your learning experience.

The Plans

The book has the content that I planned, and I’m pleased with the overall shape of this project.

Right now I plan to focus on the blog content and gather more material on C++17… and C++20. This also means that I might provide additional updates to the book (but probably not that large).

Your Feedback

I appreciate your initial feedback and support! The book has now more than 1050 readers (and only six refunds)!

Let me know what’s your experience with the book. What would you like to change? What would you like to see more?

Add your feedback/review here:
https://www.goodreads.com/book/show/41447221-c-17-in-detail

You can use this comment site:
https://leanpub.com/cpp17indetail/feedback

Or forum:
https://community.leanpub.com/c/cpp17indetail

C++17 In Detail
© 2017, Bartlomiej Filipek, Blogger platform
Disclaimer: Any opinions expressed herein are in no way representative of those of my employers. All data and information provided on this site is for informational purposes only. I try to write complete and accurate articles, but the web-site will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.