From 5d8180a8094271f64336613b091381399cc3db9d Mon Sep 17 00:00:00 2001 From: Semisonic Date: Wed, 26 Feb 2020 02:42:09 +0300 Subject: [PATCH 1/3] Update README.md (#79) * Update README.md As per https://en.cppreference.com/w/cpp/utility/integer_sequence, `std::make_integer_sequence` doesn't accept any parameter pack template arguments --- CPP14.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CPP14.md b/CPP14.md index 352d953..db17048 100644 --- a/CPP14.md +++ b/CPP14.md @@ -169,7 +169,7 @@ std::chrono::duration_cast(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` - creates a sequence of `0, ..., N - 1` with type `T`. +* `std::make_integer_sequence` - creates a sequence of `0, ..., N - 1` with type `T`. * `std::index_sequence_for` - converts a template parameter pack into an integer sequence. Convert an array into a tuple: diff --git a/README.md b/README.md index 5b747bb..ddc80a9 100644 --- a/README.md +++ b/README.md @@ -1126,7 +1126,7 @@ std::chrono::duration_cast(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` - creates a sequence of `0, ..., N - 1` with type `T`. +* `std::make_integer_sequence` - creates a sequence of `0, ..., N - 1` with type `T`. * `std::index_sequence_for` - converts a template parameter pack into an integer sequence. Convert an array into a tuple: From 187b0594209a8175d5ca0105c0c88d590e58fc5d Mon Sep 17 00:00:00 2001 From: Farsan Rashid Date: Mon, 9 Mar 2020 01:02:02 +0600 Subject: [PATCH 2/3] Add new library feature std::ref (#80) * Add std::ref Add missing library feature introduced in CPP11. * Modify comment in example of std::ref * Add comment in example of std::ref * Add link in table of content * Add new library feature std::ref under CPP11 * Add std::cref * Add std::cref --- CPP11.md | 18 ++++++++++++++++++ README.md | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CPP11.md b/CPP11.md index a12207c..88c3e42 100644 --- a/CPP11.md +++ b/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(), function_that_throws(), std::make_shared()); 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>vec; // vectorvec 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. diff --git a/README.md b/README.md index ddc80a9..e8233c2 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,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) @@ -1975,6 +1976,23 @@ foo(std::make_shared(), function_that_throws(), std::make_shared()); 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>vec; // vectorvec 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. From 352a87a284edabe7000872b47bd1a0a9cbd5ee76 Mon Sep 17 00:00:00 2001 From: Andreas Hofmann Date: Wed, 20 May 2020 01:29:39 +0200 Subject: [PATCH 3/3] Fix typo: Correct argument name in enum namespace example. (#81) Signed-off-by: Andreas Hofmann --- CPP20.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CPP20.md b/CPP20.md index 4707461..b75d8dd 100644 --- a/CPP20.md +++ b/CPP20.md @@ -329,7 +329,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"; diff --git a/README.md b/README.md index e8233c2..230b04e 100644 --- a/README.md +++ b/README.md @@ -422,7 +422,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";