mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-16 17:47:02 +03:00
Merge branch 'master' of github.com:AnthonyCalandra/modern-cpp-features
This commit is contained in:
18
CPP11.md
18
CPP11.md
@@ -48,6 +48,7 @@ C++11 includes the following new library features:
|
||||
- [std::array](#stdarray)
|
||||
- [unordered containers](#unordered-containers)
|
||||
- [std::make_shared](#stdmake_shared)
|
||||
- [std::ref](#stdref)
|
||||
- [memory model](#memory-model)
|
||||
- [std::async](#stdasync)
|
||||
- [std::begin/end](#stdbeginend)
|
||||
@@ -869,6 +870,23 @@ foo(std::make_shared<T>(), function_that_throws(), std::make_shared<T>());
|
||||
|
||||
See the section on [smart pointers](#smart-pointers) for more information on `std::unique_ptr` and `std::shared_ptr`.
|
||||
|
||||
### std::ref
|
||||
`std::ref(val)` is used to create object of type `std::reference_wrapper` that holds reference of val. Used in cases when usual reference passing using `&` does not compile or `&` is dropped due to type deduction. `std::cref` is similar but created reference wrapper holds a const reference to val.
|
||||
|
||||
```c++
|
||||
// create a container to store reference of objects.
|
||||
auto val = 99;
|
||||
auto _ref = std::ref(val);
|
||||
_ref++;
|
||||
auto _cref = std::cref(val);
|
||||
//_cref++; does not compile
|
||||
std::vector<std::reference_wrapper<int>>vec; // vector<int&>vec does not compile
|
||||
vec.push_back(_ref); // vec.push_back(&i) does not compile
|
||||
cout << val << endl; // prints 100
|
||||
cout << vec[0] << endl; // prints 100
|
||||
cout << _cref; // prints 100
|
||||
```
|
||||
|
||||
### Memory model
|
||||
C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables.
|
||||
|
||||
|
||||
2
CPP14.md
2
CPP14.md
@@ -169,7 +169,7 @@ std::chrono::duration_cast<std::chrono::minutes>(day).count(); // == 1440
|
||||
|
||||
### Compile-time integer sequences
|
||||
The class template `std::integer_sequence` represents a compile-time sequence of integers. There are a few helpers built on top:
|
||||
* `std::make_integer_sequence<T, N...>` - creates a sequence of `0, ..., N - 1` with type `T`.
|
||||
* `std::make_integer_sequence<T, N>` - creates a sequence of `0, ..., N - 1` with type `T`.
|
||||
* `std::index_sequence_for<T...>` - converts a template parameter pack into an integer sequence.
|
||||
|
||||
Convert an array into a tuple:
|
||||
|
||||
2
CPP20.md
2
CPP20.md
@@ -333,7 +333,7 @@ After:
|
||||
```c++
|
||||
enum class rgba_color_channel { red, green, blue, alpha };
|
||||
|
||||
std::string_view to_string(rgba_color_channel channel) {
|
||||
std::string_view to_string(rgba_color_channel my_channel) {
|
||||
switch (my_channel) {
|
||||
using enum rgba_color_channel;
|
||||
case red: return "red";
|
||||
|
||||
22
README.md
22
README.md
@@ -117,6 +117,7 @@ C++11 includes the following new library features:
|
||||
- [std::array](#stdarray)
|
||||
- [unordered containers](#unordered-containers)
|
||||
- [std::make_shared](#stdmake_shared)
|
||||
- [std::ref](#stdref)
|
||||
- [memory model](#memory-model)
|
||||
- [std::async](#stdasync)
|
||||
- [std::begin/end](#stdbeginend)
|
||||
@@ -425,7 +426,7 @@ After:
|
||||
```c++
|
||||
enum class rgba_color_channel { red, green, blue, alpha };
|
||||
|
||||
std::string_view to_string(rgba_color_channel channel) {
|
||||
std::string_view to_string(rgba_color_channel my_channel) {
|
||||
switch (my_channel) {
|
||||
using enum rgba_color_channel;
|
||||
case red: return "red";
|
||||
@@ -1163,7 +1164,7 @@ std::chrono::duration_cast<std::chrono::minutes>(day).count(); // == 1440
|
||||
|
||||
### Compile-time integer sequences
|
||||
The class template `std::integer_sequence` represents a compile-time sequence of integers. There are a few helpers built on top:
|
||||
* `std::make_integer_sequence<T, N...>` - creates a sequence of `0, ..., N - 1` with type `T`.
|
||||
* `std::make_integer_sequence<T, N>` - creates a sequence of `0, ..., N - 1` with type `T`.
|
||||
* `std::index_sequence_for<T...>` - converts a template parameter pack into an integer sequence.
|
||||
|
||||
Convert an array into a tuple:
|
||||
@@ -2012,6 +2013,23 @@ foo(std::make_shared<T>(), function_that_throws(), std::make_shared<T>());
|
||||
|
||||
See the section on [smart pointers](#smart-pointers) for more information on `std::unique_ptr` and `std::shared_ptr`.
|
||||
|
||||
### std::ref
|
||||
`std::ref(val)` is used to create object of type `std::reference_wrapper` that holds reference of val. Used in cases when usual reference passing using `&` does not compile or `&` is dropped due to type deduction. `std::cref` is similar but created reference wrapper holds a const reference to val.
|
||||
|
||||
```c++
|
||||
// create a container to store reference of objects.
|
||||
auto val = 99;
|
||||
auto _ref = std::ref(val);
|
||||
_ref++;
|
||||
auto _cref = std::cref(val);
|
||||
//_cref++; does not compile
|
||||
std::vector<std::reference_wrapper<int>>vec; // vector<int&>vec does not compile
|
||||
vec.push_back(_ref); // vec.push_back(&i) does not compile
|
||||
cout << val << endl; // prints 100
|
||||
cout << vec[0] << endl; // prints 100
|
||||
cout << _cref; // prints 100
|
||||
```
|
||||
|
||||
### Memory model
|
||||
C++11 introduces a memory model for C++, which means library support for threading and atomic operations. Some of these operations include (but aren't limited to) atomic loads/stores, compare-and-swap, atomic flags, promises, futures, locks, and condition variables.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user