From af7125a698913c87436e91f3288bc57b4eacc9b5 Mon Sep 17 00:00:00 2001 From: Azam Bham <44165939+ABham2997@users.noreply.github.com> Date: Sat, 21 Sep 2019 21:30:04 +0100 Subject: [PATCH 1/5] "Template aliases" have been renamed to "Type aliases" in CPP11.md and README.md files (#60) * "template aliases" have been renamed to "type aliases". --- CPP11.md | 6 +++--- README.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CPP11.md b/CPP11.md index b9bfd8c..ecc387a 100644 --- a/CPP11.md +++ b/CPP11.md @@ -13,7 +13,7 @@ C++11 includes the following new language features: - [auto](#auto) - [lambda expressions](#lambda-expressions) - [decltype](#decltype) -- [template aliases](#template-aliases) +- [type aliases](#type-aliases) - [nullptr](#nullptr) - [strongly-typed enums](#strongly-typed-enums) - [attributes](#attributes) @@ -243,8 +243,8 @@ add(1, 2.0); // `decltype(x + y)` => `decltype(3.0)` => `double` See also: `decltype(auto)` (C++14). -### Template aliases -Semantically similar to using a `typedef` however, template aliases with `using` are easier to read and are compatible with templates. +### Type aliases +Semantically similar to using a `typedef` however, type aliases with `using` are easier to read and are compatible with templates. ```c++ template using Vec = std::vector; diff --git a/README.md b/README.md index c82108a..a75470f 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ C++11 includes the following new language features: - [auto](#auto) - [lambda expressions](#lambda-expressions) - [decltype](#decltype) -- [template aliases](#template-aliases) +- [type aliases](#type-aliases) - [nullptr](#nullptr) - [strongly-typed enums](#strongly-typed-enums) - [attributes](#attributes) @@ -1054,8 +1054,8 @@ add(1, 2.0); // `decltype(x + y)` => `decltype(3.0)` => `double` See also: [`decltype(auto)`](#decltypeauto). -### Template aliases -Semantically similar to using a `typedef` however, template aliases with `using` are easier to read and are compatible with templates. +### Type aliases +Semantically similar to using a `typedef` however, type aliases with `using` are easier to read and are compatible with templates. ```c++ template using Vec = std::vector; From cdcd516ae826658c2104a611e7c0f58d31d6a9c3 Mon Sep 17 00:00:00 2001 From: Matthew Guidry Date: Mon, 14 Oct 2019 10:41:00 -0500 Subject: [PATCH 2/5] Add More Examples to Some Sections (#63) * Add example to Structured Bindings --- CPP17.md | 12 ++++++++++++ README.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CPP17.md b/CPP17.md index 7f3b0bd..340010f 100644 --- a/CPP17.md +++ b/CPP17.md @@ -178,6 +178,18 @@ const auto [ x, y ] = origin(); x; // == 0 y; // == 0 ``` +```c++ +std::unordered_map mapping { + {"a", 1}, + {"b", 2}, + {"c", 3}, +}; + +// Destructure by reference. +for (const auto& [key, value] : mapping) { + // Do something with key and value +} +``` ### Selection statements with initializer New versions of the `if` and `switch` statements which simplify common code patterns and help users keep scopes tight. diff --git a/README.md b/README.md index a75470f..dd96f20 100644 --- a/README.md +++ b/README.md @@ -429,6 +429,18 @@ const auto [ x, y ] = origin(); x; // == 0 y; // == 0 ``` +```c++ +std::unordered_map mapping { + {"a", 1}, + {"b", 2}, + {"c", 3}, +}; + +// Destructure by reference. +for (const auto& [key, value] : mapping) { + // Do something with key and value +} +``` ### Selection statements with initializer New versions of the `if` and `switch` statements which simplify common code patterns and help users keep scopes tight. From 4eb1a3ff5fd6cc93ff4bff3bf23c8f6db5e169eb Mon Sep 17 00:00:00 2001 From: Matthew Guidry Date: Thu, 24 Oct 2019 17:40:38 -0500 Subject: [PATCH 3/5] Parameter Pack to Initializer List (#65) * Add alternate use variadic template example --- CPP11.md | 13 +++++++++++++ README.md | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CPP11.md b/CPP11.md index ecc387a..a719524 100644 --- a/CPP11.md +++ b/CPP11.md @@ -128,6 +128,19 @@ static_assert(arity<>::value == 0); static_assert(arity::value == 3); ``` +An interesting use for this is creating an _initializer list_ from a _parameter pack_ in order to iterate over variadic function arguments. +```c++ +template +auto sum(const First first, const Args... args) -> decltype(first) { + const auto values = {first, args...}; + return std::accumulate(values.begin(), values.end(), First{0}); +} + +sum(1, 2, 3, 4, 5); // 15 +sum(1, 2, 3); // 6 +sum(1.5, 2.0, 3.7); // 7.2 +``` + ### Initializer lists A lightweight array-like container of elements created using a "braced list" syntax. For example, `{ 1, 2, 3 }` creates a sequences of integers, that has type `std::initializer_list`. Useful as a replacement to passing a vector of objects to a function. ```c++ diff --git a/README.md b/README.md index dd96f20..4625d72 100644 --- a/README.md +++ b/README.md @@ -951,6 +951,19 @@ static_assert(arity<>::value == 0); static_assert(arity::value == 3); ``` +An interesting use for this is creating an _initializer list_ from a _parameter pack_ in order to iterate over variadic function arguments. +```c++ +template +auto sum(const First first, const Args... args) -> decltype(first) { + const auto values = {first, args...}; + return std::accumulate(values.begin(), values.end(), First{0}); +} + +sum(1, 2, 3, 4, 5); // 15 +sum(1, 2, 3); // 6 +sum(1.5, 2.0, 3.7); // 7.2 +``` + ### Initializer lists A lightweight array-like container of elements created using a "braced list" syntax. For example, `{ 1, 2, 3 }` creates a sequences of integers, that has type `std::initializer_list`. Useful as a replacement to passing a vector of objects to a function. ```c++ From b785d8f9717a5521e2857d79cc9e477833e54c9a Mon Sep 17 00:00:00 2001 From: Muhammet Ali Asan Date: Thu, 14 Nov 2019 18:24:14 +0100 Subject: [PATCH 4/5] added std::begin/end (#69) * added std::begin/end --- CPP11.md | 18 ++++++++++++++++++ README.md | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CPP11.md b/CPP11.md index a719524..18e7488 100644 --- a/CPP11.md +++ b/CPP11.md @@ -49,6 +49,7 @@ C++11 includes the following new library features: - [std::make_shared](#stdmake_shared) - [memory model](#memory-model) - [std::async](#stdasync) +- [std::begin/end](#stdbeginend) ## C++11 Language Features @@ -874,6 +875,23 @@ auto handle = std::async(std::launch::async, foo); // create an async task auto result = handle.get(); // wait for the result ``` +### std::begin/end +`std::begin` and `std::end` free functions were added to return begin and end iterators of a container generically. These functions also work with raw arrays which do not have begin and end member functions. + +```c++ +template +int CountTwos(const T& container) { + return std::count_if(std::begin(container), std::end(container), [](int item) { + return item == 2; + }); +} + +std::vector vec = {2,2,43,435,4543,534}; +int arr[8] = {2,43,45,435,32,32,32,32}; +auto a = CountTwos(vec); // 2 +auto b = CountTwos(arr); // 1 +``` + ## 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 4625d72..c645ec3 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ C++11 includes the following new library features: - [std::make_shared](#stdmake_shared) - [memory model](#memory-model) - [std::async](#stdasync) +- [std::begin/end](#stdbeginend) ## C++20 Language Features @@ -1698,6 +1699,23 @@ auto handle = std::async(std::launch::async, foo); // create an async task auto result = handle.get(); // wait for the result ``` +### std::begin/end +`std::begin` and `std::end` free functions were added to return begin and end iterators of a container generically. These functions also work with raw arrays which do not have begin and end member functions. + +```c++ +template +int CountTwos(const T& container) { + return std::count_if(std::begin(container), std::end(container), [](int item) { + return item == 2; + }); +} + +std::vector vec = {2,2,43,435,4543,534}; +int arr[8] = {2,43,45,435,32,32,32,32}; +auto a = CountTwos(vec); // 2 +auto b = CountTwos(arr); // 1 +``` + ## 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. From 07b1193de0d90640827f05a4233f11c0cb721469 Mon Sep 17 00:00:00 2001 From: Matthew Guidry Date: Mon, 16 Dec 2019 20:37:24 -0600 Subject: [PATCH 5/5] Add section about noexcept specifier (#72) * Add section about noexcept --- CPP11.md | 22 ++++++++++++++++++++++ README.md | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/CPP11.md b/CPP11.md index 18e7488..1f86b18 100644 --- a/CPP11.md +++ b/CPP11.md @@ -33,6 +33,7 @@ C++11 includes the following new language features: - [right angle brackets](#right-angle-brackets) - [ref-qualified member functions](#ref-qualified-member-functions) - [trailing return types](#trailing-return-types) +- [noexcept specifier](#noexcept-specifier) C++11 includes the following new library features: - [std::move](#stdmove) @@ -659,6 +660,27 @@ auto add(T a, U b) -> decltype(a + b) { ``` In C++14, `decltype(auto)` can be used instead. +### Noexcept specifier +The `noexcept` specifier specifies whether a function could throw exceptions. It is an improved version of `throw()`. + +```c++ +void func1() noexcept; // does not throw +void func2() noexcept(true); // does not throw +void func3() throw(); // does not throw + +void func4() noexcept(false); // may throw +``` + +Non-throwing functions are permitted to call potentially-throwing functions. Whenever an exception is thrown and the search for a handler encounters the outermost block of a non-throwing function, the function std::terminate is called. + +```c++ +extern void f(); // potentially-throwing +void g() noexcept { + f(); // valid, even if f throws + throw 42; // valid, effectively a call to std::terminate +} +``` + ## C++11 Library Features ### std::move diff --git a/README.md b/README.md index c645ec3..bd8cef6 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ C++11 includes the following new language features: - [right angle brackets](#right-angle-brackets) - [ref-qualified member functions](#ref-qualified-member-functions) - [trailing return types](#trailing-return-types) +- [noexcept specifier](#noexcept-specifier) C++11 includes the following new library features: - [std::move](#stdmove) @@ -1483,6 +1484,27 @@ auto add(T a, U b) -> decltype(a + b) { ``` In C++14, [decltype(auto)](#decltypeauto) can be used instead. +### Noexcept specifier +The `noexcept` specifier specifies whether a function could throw exceptions. It is an improved version of `throw()`. + +```c++ +void func1() noexcept; // does not throw +void func2() noexcept(true); // does not throw +void func3() throw(); // does not throw + +void func4() noexcept(false); // may throw +``` + +Non-throwing functions are permitted to call potentially-throwing functions. Whenever an exception is thrown and the search for a handler encounters the outermost block of a non-throwing function, the function std::terminate is called. + +```c++ +extern void f(); // potentially-throwing +void g() noexcept { + f(); // valid, even if f throws + throw 42; // valid, effectively a call to std::terminate +} +``` + ## C++11 Library Features ### std::move