From 0e24601c4f1434a0b28bc3d4bb38bda2d4111c8e Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Thu, 25 Jun 2020 21:40:24 -0400 Subject: [PATCH] Support for: - std::make_shared with C-style arrays - starts_with and ends_with - contains - std::bit_cast --- CPP20.md | 37 +++++++++++++++++++++++++++++++++++++ README.md | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/CPP20.md b/CPP20.md index 4707461..19980e9 100644 --- a/CPP20.md +++ b/CPP20.md @@ -24,6 +24,10 @@ C++20 includes the following new library features: - [bit operations](#bit-operations) - [math constants](#math-constants) - [std::is_constant_evaluated](#stdis_constant_evaluated) +- [std::make_shared supports arrays](#stdmake_shared-supports-array) +- [starts_with and ends_with on strings](#starts_with-and-ends_with-on-strings) +- [check if associative container has element](#check-if-associative-container-has-element) +- [std::bit_cast](#stdbit_cast) ## C++20 Language Features @@ -438,6 +442,39 @@ constexpr bool a = is_compile_time(); // true bool b = is_compile_time(); // false ``` +### std::make_shared supports arrays +```c++ +auto p = std::make_shared(5); // pointer to `int[5]` +// OR +auto p = std::make_shared(); // pointer to `int[5]` +``` + +### starts_with and ends_with +Strings (and string views) now have the `starts_with` and `ends_with` member functions to check if a string starts or ends with the given string. +```c++ +std::string str = "foobar"; +str.starts_with("foo"); // true +str.ends_with("baz"); // false +``` + +### Check if associative container has element +Associative containers such as sets and maps have a `contains` member function, which can be used instead of the "find and check end of iterator" idiom. +```c++ +std::map map {{1, 'a'}, {2, 'b'}}; +map.contains(2); // true +map.contains(123); // false + +std::set set {1, 2, 3}; +set.contains(2); // true +``` + +### std::bit_cast +A safer way to reinterpret an object from one type to another. +```c++ +float f = 123.0; +int i = std::bit_cast(f); +``` + ## Acknowledgements * [cppreference](http://en.cppreference.com/w/cpp) - especially useful for finding examples and documentation of new library features. * [C++ Rvalue References Explained](http://thbecker.net/articles/rvalue_references/section_01.html) - a great introduction I used to understand rvalue references, perfect forwarding, and move semantics. diff --git a/README.md b/README.md index 5b747bb..acb5ab8 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ C++20 includes the following new library features: - [bit operations](#bit-operations) - [math constants](#math-constants) - [std::is_constant_evaluated](#stdis_constant_evaluated) +- [std::make_shared supports arrays](#stdmake_shared-supports-array) +- [starts_with and ends_with on strings](#starts_with-and-ends_with-on-strings) +- [check if associative container has element](#check-if-associative-container-has-element) +- [std::bit_cast](#stdbit_cast) C++17 includes the following new language features: - [template argument deduction for class templates](#template-argument-deduction-for-class-templates) @@ -530,6 +534,39 @@ constexpr bool a = is_compile_time(); // true bool b = is_compile_time(); // false ``` +### std::make_shared supports arrays +```c++ +auto p = std::make_shared(5); // pointer to `int[5]` +// OR +auto p = std::make_shared(); // pointer to `int[5]` +``` + +### starts_with and ends_with +Strings (and string views) now have the `starts_with` and `ends_with` member functions to check if a string starts or ends with the given string. +```c++ +std::string str = "foobar"; +str.starts_with("foo"); // true +str.ends_with("baz"); // false +``` + +### Check if associative container has element +Associative containers such as sets and maps have a `contains` member function, which can be used instead of the "find and check end of iterator" idiom. +```c++ +std::map map {{1, 'a'}, {2, 'b'}}; +map.contains(2); // true +map.contains(123); // false + +std::set set {1, 2, 3}; +set.contains(2); // true +``` + +### std::bit_cast +A safer way to reinterpret an object from one type to another. +```c++ +float f = 123.0; +int i = std::bit_cast(f); +``` + ## C++17 Language Features ### Template argument deduction for class templates