mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-16 17:47:02 +03:00
Support for:
- std::make_shared with C-style arrays - starts_with and ends_with - contains - std::bit_cast
This commit is contained in:
37
CPP20.md
37
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<int[]>(5); // pointer to `int[5]`
|
||||
// OR
|
||||
auto p = std::make_shared<int[5]>(); // 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<int, char> map {{1, 'a'}, {2, 'b'}};
|
||||
map.contains(2); // true
|
||||
map.contains(123); // false
|
||||
|
||||
std::set<int> 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<int>(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.
|
||||
|
||||
37
README.md
37
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<int[]>(5); // pointer to `int[5]`
|
||||
// OR
|
||||
auto p = std::make_shared<int[5]>(); // 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<int, char> map {{1, 'a'}, {2, 'b'}};
|
||||
map.contains(2); // true
|
||||
map.contains(123); // false
|
||||
|
||||
std::set<int> 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<int>(f);
|
||||
```
|
||||
|
||||
## C++17 Language Features
|
||||
|
||||
### Template argument deduction for class templates
|
||||
|
||||
Reference in New Issue
Block a user