From d386a48f3b5b637a6b625d82b7b53c447d0ee1ad Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Thu, 23 Aug 2018 22:45:05 -0400 Subject: [PATCH] Add std::byte. --- CPP17.md | 46 +++++++++++++++++++++++++++++----------------- README.md | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/CPP17.md b/CPP17.md index 1c81c6e..357de77 100644 --- a/CPP17.md +++ b/CPP17.md @@ -25,8 +25,9 @@ C++17 includes the following new library features: - [std::string_view](#stdstring_view) - [std::invoke](#stdinvoke) - [std::apply](#stdapply) -- [splicing for maps and sets](#splicing-for-maps-and-sets) - [std::filesystem](#stdfilesystem) +- [std::byte](#stdbyte) +- [splicing for maps and sets](#splicing-for-maps-and-sets) ## C++17 Language Features @@ -326,6 +327,33 @@ auto add = [] (int x, int y) { std::apply(add, std::make_tuple( 1, 2 )); // == 3 ``` +### std::filesystem +The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. + +Here, a big file is copied to a temporary path if there is available space: +```c++ +const auto bigFilePath {"bigFileToCopy"}; +if (std::filesystem::exists(bigFilePath)) { + const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; + std::filesystem::path tmpPath {"/tmp"}; + if (std::filesystem::space(tmpPath).available > bigFileSize) { + std::filesystem::create_directory(tmpPath.append("example")); + std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); + } +} +``` + +### std::byte +The new `std::byte` type provides a standard way of representing data as a byte. Benefits of using `std::byte` over `char` or `unsigned char` is that it is not a character type, and is also not an arithmetic type; while the only operator overloads available are bitwise operations. +```c++ +std::byte a {0}; +std::byte b {0xFF}; +int i = std::to_integer(b); // 0xFF +std::byte c = a & b; +int j = std::to_integer(c); // 0 +``` +Note that `std::byte` is simply an enum, and braced initialization of enums become possible thanks to [direct-list-initialization of enums](#direct-list-initialization-of-enums). + ### Splicing for maps and sets Moving nodes and merging containers without the overhead of expensive copies, moves, or heap allocations/deallocations. @@ -366,22 +394,6 @@ m.insert(std::move(e)); // m == { { 1, "one" }, { 3, "three" }, { 4, "two" } } ``` -### std::filesystem -The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. - -Here, a big file is copied to a temporary path if there is available space: -```c++ -const auto bigFilePath {"bigFileToCopy"}; -if (std::filesystem::exists(bigFilePath)) { - const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; - std::filesystem::path tmpPath {"/tmp"}; - if (std::filesystem::space(tmpPath).available > bigFileSize) { - std::filesystem::create_directory(tmpPath.append("example")); - std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); - } -} -``` - ## 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 f33977e..b553f8c 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,9 @@ C++17 includes the following new library features: - [std::string_view](#stdstring_view) - [std::invoke](#stdinvoke) - [std::apply](#stdapply) -- [splicing for maps and sets](#splicing-for-maps-and-sets) - [std::filesystem](#stdfilesystem) +- [std::byte](#stdbyte) +- [splicing for maps and sets](#splicing-for-maps-and-sets) C++14 includes the following new language features: - [binary literals](#binary-literals) @@ -385,6 +386,33 @@ auto add = [] (int x, int y) { std::apply(add, std::make_tuple( 1, 2 )); // == 3 ``` +### std::filesystem +The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. + +Here, a big file is copied to a temporary path if there is available space: +```c++ +const auto bigFilePath {"bigFileToCopy"}; +if (std::filesystem::exists(bigFilePath)) { + const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; + std::filesystem::path tmpPath {"/tmp"}; + if (std::filesystem::space(tmpPath).available > bigFileSize) { + std::filesystem::create_directory(tmpPath.append("example")); + std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); + } +} +``` + +### std::byte +The new `std::byte` type provides a standard way of representing data as a byte. Benefits of using `std::byte` over `char` or `unsigned char` is that it is not a character type, and is also not an arithmetic type; while the only operator overloads available are bitwise operations. +```c++ +std::byte a {0}; +std::byte b {0xFF}; +int i = std::to_integer(b); // 0xFF +std::byte c = a & b; +int j = std::to_integer(c); // 0 +``` +Note that `std::byte` is simply an enum, and braced initialization of enums become possible thanks to [direct-list-initialization of enums](#direct-list-initialization-of-enums). + ### Splicing for maps and sets Moving nodes and merging containers without the overhead of expensive copies, moves, or heap allocations/deallocations. @@ -424,21 +452,6 @@ e.key() = 4; m.insert(std::move(e)); // m == { { 1, "one" }, { 3, "three" }, { 4, "two" } } ``` -### std::filesystem -The new `std::filesystem` library provides a standard way to manipulate files, directories, and paths in a filesystem. - -Here, a big file is copied to a temporary path if there is available space: -```c++ -const auto bigFilePath {"bigFileToCopy"}; -if (std::filesystem::exists(bigFilePath)) { - const auto bigFileSize {std::filesystem::file_size(bigFilePath)}; - std::filesystem::path tmpPath {"/tmp"}; - if (std::filesystem::space(tmpPath).available > bigFileSize) { - std::filesystem::create_directory(tmpPath.append("example")); - std::filesystem::copy_file(bigFilePath, tmpPath.append("newFile")); - } -} -``` ## C++14 Language Features