A brief overview of C++17
Daniel Eriksson
Table of contents
● The standardization process, the committee
● C++ Standards
● The current status of the standardization process
● New language features
● New features in the standard libraries
● References
The standardization process
● A Commitee under ISO/IEC so multiple companies can co-operate and define
the language
● Work Group 21 (Google for wg21)
● Info on isocpp.org
○ Community site for publicizing for publicizing activity
● Work with Technical Specifications, TS:s
C++ Standards
● 1998
● 2003 TC1
● 2011
● 2014
● 2017
The Current Status
● Builds on C++14
● Significant library update
○ The additions and changes to the libraries are the biggest part of the new standard.
● Modest language update
● The standard has been agreed upon
● Will now be reviewed by ISO
● Expected release by the end of 2017
New language features
Attributes
● [[fallthrough]] a case without breaking
● [[nodiscard]] warns on ignored function results
● [[maybe_unused]]
● Grammar supports attributes on namespaces
○ namespace std::relops [[deprecated]] { …. }
● Grammars supports attributes on enumeratiors
○ enum MyEnum {old_name [[deprecated]], new_name ]
New Language Features
Examples
● [[maybe_unsued]] int f() {} // Won’t issue warning if return value is not
catched.
● [[nodiscard]] int f() {} // Will issue warning if return value is not catched.
● Alos deducted from type:
○ struct [[nodiscard]] MyStruct {};
○ MyStruct f() {}; // Will give compiler warning if return value is not handled.
New Language Features
Lamda Expressions
● constexpr lamda expressions
● It is now possible to caputre a copy of this, e.g *this
New Language Features
Structured Bindings
● Declare multiple variables, bound by function result
○ auto [x, y] = *map.find(key) // Returns a pair
● Functions can return
○ An aggregate
○ an arry-by-reference
○ something that supports the tuple protocol: array, pair or tuple
● Works anywhere an initialization may be performed
for (auto& [first, second] : myMap) {
// use firts and secon
}
New Features In The Standard Libraries
● Multicore support and Parallelism 1
● Math functions
● Extended vocabulary
● Text Handling
● Filesystem
● Smart Pointers
Parallelism
Distinction between parallelism and concurrency
● Parallelism
○ Simultaneously executing many copies of the same task to speed a single computation
● Concurrency
○ Performing multiple actions at the same time, often to improve latency
Parallelism
● Added execution_policy overload to most functions in the <algorithm> and <numeric>
headers
● Almost all of the algorithms in the standard library now how a “paralliezed” version
● Excpet
○ Random nubers
○ Heap opertaions (other than is_hep and is_heap_until)
○ permutations operations
○ copy_backwards, move_backward, lowe_bound, upper_bound, equal_range,
binary_search, accumulate, partial_sum, iota
● Added some new
○ exclusive_scan
○ inclusive_scan
○ transform_reduce
○ redue
Parallellism
Execution policies
● std::execution::seq
● std::execution::par
● std::execution::par_unseq
Math functions
● Adopted all of ISO 29124 (extensions to C++)
○ > 20 mathematical special functions
○ Bessel functions, beta function, riemann zeta etc
● hypot(x, y, z)
● gcd(a, b)
● lcm(a, b)
● A sampling function
○ sample(begin, end, out_iter, nSamples, rgb)
pair and tuple
● Unpack tuple arguments into a function call
○ apply(func, tuple{1, “two”, 3.14});
● Construct and object by unpacking a tuple
○ auto x = tuple{1, “two”, 3.14}
○ auto y = make_from_tuple<MyTYpe>(x);
Extended vocabulary
● Vocabulary types are the kind of types you would like to use in your interfaces
● Good with standardized vocabulary between libraries (for example)
● Already present
○ pair, tuple, array already in
● New ones
○ optinal<T>
○ any
○ variant<Types…>
○ string_view
optional<T>
#include <variant>
#include <string>
#include <string>
#include <iostream>
#include <optional>
// optional can be used as the return type of a factory that may fail
std::optional<std::string> create(bool b) {
if(b)
return "Godzilla";
else
return {};
}
int main()
{
std::cout << "create(false) returned "
<< create(false).value_or("empty") << 'n';
// optional-returning factory functions are usable as conditions of while and if
if(auto str = create(true)) {
std::cout << "create(true) returned " << *str << 'n';
}
}
variant<TYPES…>
#include <variant>
#include <string>
int main()
{
std::variant<int, float> v, w;
v = 12; // v contains int
int i = std::get<int>(v);
w = std::get<int>(v);
w = std::get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line
// std::get<double>(v); // error: no double in [int, float]
// std::get<3>(v); // error: valid index values are 0 and 1
try {
std::get<float>(w); // w contains int, not float: will throw
}
catch (std::bad_variant_access&) {}
std::variant<std::string> v("abc"); // converting constructors work when unambiguous
v = "def"; // converting assignment also works when unambiguous
}
any
● Can hold any value as long as it is copy constructible
● Like varian except that it does not know what type it holds
● Will have to use any_cast to retrieve the value
any
int main()
{
boost::any a = 1;
std::cout << std::any_cast<int>(a) << 'n';
a = 3.14;
std::cout << std::any_cast<double>(a) << 'n';
a = true;
std::cout << std::boolalpha << std::any_cast<bool>(a) << 'n';
}
string_view
● string_view gives the ability to refer to an existing string in a non-owning way.
bool compare(const std::string& s1, const std::string& s2)
{
// do some comparisons between s1 and s2
}
int main()
{
std::string str = "this is my input string";
bool r1 = compare(str, "this is the first test string");
bool r2 = compare(str, "this is the second test string");
bool r3 = compare(str, "this is the third test string");
}
string_view
bool compare(std::string_view s1, std::string_view s2)
{
if (s1 == s2)
return true;
std::cout << '"' << s1 << "" does not match "" << s2 << ""n";
return false;
}
int main()
{
std::string str = "this is my input string";
compare(str, "this is the first test string");
compare(str, "this is the second test string");
compare(str, "this is the third test string");
return 0;
}
string_view
● In the last example only str is allocated.
● It is also possible to create string_view from a substring of a string.
○ The string_view points into the original string rather than to allocate a new string
○ string_view holds a pointer to a string and a size
Filesystem
● std::filesystem
● Specification based on POSIX standard semantics
○ NO protection against file system data races
● Uses system_error reporting introduced in C++11
● Functions and iterators to navigate a filesystem
● Functions to create, manipulate and query files (including directories and
symlinks)
Further Changes And Additions
● Smart pointers
● Allocators
● Memory Resources
Library Features Removed
● auto_ptr => Use unique_ptr
● bind1st, bind2nd, men_fun, men_fun_ref, ptr_fun, unary_function,
binary_funciton
● random_shuffle
References
● Alisdair Meredith
○ C++17 in Breadth Part 1
○ C++17 in Breadth Part 2
● isocpp.org
● cppreference.com

Meetup C++ A brief overview of c++17

  • 1.
    A brief overviewof C++17 Daniel Eriksson
  • 2.
    Table of contents ●The standardization process, the committee ● C++ Standards ● The current status of the standardization process ● New language features ● New features in the standard libraries ● References
  • 3.
    The standardization process ●A Commitee under ISO/IEC so multiple companies can co-operate and define the language ● Work Group 21 (Google for wg21) ● Info on isocpp.org ○ Community site for publicizing for publicizing activity ● Work with Technical Specifications, TS:s
  • 4.
    C++ Standards ● 1998 ●2003 TC1 ● 2011 ● 2014 ● 2017
  • 5.
    The Current Status ●Builds on C++14 ● Significant library update ○ The additions and changes to the libraries are the biggest part of the new standard. ● Modest language update ● The standard has been agreed upon ● Will now be reviewed by ISO ● Expected release by the end of 2017
  • 7.
    New language features Attributes ●[[fallthrough]] a case without breaking ● [[nodiscard]] warns on ignored function results ● [[maybe_unused]] ● Grammar supports attributes on namespaces ○ namespace std::relops [[deprecated]] { …. } ● Grammars supports attributes on enumeratiors ○ enum MyEnum {old_name [[deprecated]], new_name ]
  • 8.
    New Language Features Examples ●[[maybe_unsued]] int f() {} // Won’t issue warning if return value is not catched. ● [[nodiscard]] int f() {} // Will issue warning if return value is not catched. ● Alos deducted from type: ○ struct [[nodiscard]] MyStruct {}; ○ MyStruct f() {}; // Will give compiler warning if return value is not handled.
  • 9.
    New Language Features LamdaExpressions ● constexpr lamda expressions ● It is now possible to caputre a copy of this, e.g *this
  • 10.
    New Language Features StructuredBindings ● Declare multiple variables, bound by function result ○ auto [x, y] = *map.find(key) // Returns a pair ● Functions can return ○ An aggregate ○ an arry-by-reference ○ something that supports the tuple protocol: array, pair or tuple ● Works anywhere an initialization may be performed for (auto& [first, second] : myMap) { // use firts and secon }
  • 11.
    New Features InThe Standard Libraries ● Multicore support and Parallelism 1 ● Math functions ● Extended vocabulary ● Text Handling ● Filesystem ● Smart Pointers
  • 12.
    Parallelism Distinction between parallelismand concurrency ● Parallelism ○ Simultaneously executing many copies of the same task to speed a single computation ● Concurrency ○ Performing multiple actions at the same time, often to improve latency
  • 13.
    Parallelism ● Added execution_policyoverload to most functions in the <algorithm> and <numeric> headers ● Almost all of the algorithms in the standard library now how a “paralliezed” version ● Excpet ○ Random nubers ○ Heap opertaions (other than is_hep and is_heap_until) ○ permutations operations ○ copy_backwards, move_backward, lowe_bound, upper_bound, equal_range, binary_search, accumulate, partial_sum, iota ● Added some new ○ exclusive_scan ○ inclusive_scan ○ transform_reduce ○ redue
  • 14.
    Parallellism Execution policies ● std::execution::seq ●std::execution::par ● std::execution::par_unseq
  • 15.
    Math functions ● Adoptedall of ISO 29124 (extensions to C++) ○ > 20 mathematical special functions ○ Bessel functions, beta function, riemann zeta etc ● hypot(x, y, z) ● gcd(a, b) ● lcm(a, b) ● A sampling function ○ sample(begin, end, out_iter, nSamples, rgb)
  • 16.
    pair and tuple ●Unpack tuple arguments into a function call ○ apply(func, tuple{1, “two”, 3.14}); ● Construct and object by unpacking a tuple ○ auto x = tuple{1, “two”, 3.14} ○ auto y = make_from_tuple<MyTYpe>(x);
  • 17.
    Extended vocabulary ● Vocabularytypes are the kind of types you would like to use in your interfaces ● Good with standardized vocabulary between libraries (for example) ● Already present ○ pair, tuple, array already in ● New ones ○ optinal<T> ○ any ○ variant<Types…> ○ string_view
  • 18.
    optional<T> #include <variant> #include <string> #include<string> #include <iostream> #include <optional> // optional can be used as the return type of a factory that may fail std::optional<std::string> create(bool b) { if(b) return "Godzilla"; else return {}; } int main() { std::cout << "create(false) returned " << create(false).value_or("empty") << 'n'; // optional-returning factory functions are usable as conditions of while and if if(auto str = create(true)) { std::cout << "create(true) returned " << *str << 'n'; } }
  • 19.
    variant<TYPES…> #include <variant> #include <string> intmain() { std::variant<int, float> v, w; v = 12; // v contains int int i = std::get<int>(v); w = std::get<int>(v); w = std::get<0>(v); // same effect as the previous line w = v; // same effect as the previous line // std::get<double>(v); // error: no double in [int, float] // std::get<3>(v); // error: valid index values are 0 and 1 try { std::get<float>(w); // w contains int, not float: will throw } catch (std::bad_variant_access&) {} std::variant<std::string> v("abc"); // converting constructors work when unambiguous v = "def"; // converting assignment also works when unambiguous }
  • 20.
    any ● Can holdany value as long as it is copy constructible ● Like varian except that it does not know what type it holds ● Will have to use any_cast to retrieve the value
  • 21.
    any int main() { boost::any a= 1; std::cout << std::any_cast<int>(a) << 'n'; a = 3.14; std::cout << std::any_cast<double>(a) << 'n'; a = true; std::cout << std::boolalpha << std::any_cast<bool>(a) << 'n'; }
  • 22.
    string_view ● string_view givesthe ability to refer to an existing string in a non-owning way. bool compare(const std::string& s1, const std::string& s2) { // do some comparisons between s1 and s2 } int main() { std::string str = "this is my input string"; bool r1 = compare(str, "this is the first test string"); bool r2 = compare(str, "this is the second test string"); bool r3 = compare(str, "this is the third test string"); }
  • 23.
    string_view bool compare(std::string_view s1,std::string_view s2) { if (s1 == s2) return true; std::cout << '"' << s1 << "" does not match "" << s2 << ""n"; return false; } int main() { std::string str = "this is my input string"; compare(str, "this is the first test string"); compare(str, "this is the second test string"); compare(str, "this is the third test string"); return 0; }
  • 24.
    string_view ● In thelast example only str is allocated. ● It is also possible to create string_view from a substring of a string. ○ The string_view points into the original string rather than to allocate a new string ○ string_view holds a pointer to a string and a size
  • 25.
    Filesystem ● std::filesystem ● Specificationbased on POSIX standard semantics ○ NO protection against file system data races ● Uses system_error reporting introduced in C++11 ● Functions and iterators to navigate a filesystem ● Functions to create, manipulate and query files (including directories and symlinks)
  • 26.
    Further Changes AndAdditions ● Smart pointers ● Allocators ● Memory Resources
  • 27.
    Library Features Removed ●auto_ptr => Use unique_ptr ● bind1st, bind2nd, men_fun, men_fun_ref, ptr_fun, unary_function, binary_funciton ● random_shuffle
  • 28.
    References ● Alisdair Meredith ○C++17 in Breadth Part 1 ○ C++17 in Breadth Part 2 ● isocpp.org ● cppreference.com