Book-en-us
- Preface
- Chapter 01: Towards Modern C++
- Chapter 02: Language Usability Enhancements
- Chapter 03: Language Runtime Enhancements
- Chapter 04 Containers
- Chapter 05 Smart Pointers and Memory Management
- Chapter 06 Regular Expression
- Chapter 07 Parallelism and Concurrency
- Chapter 08 File System
- Chapter 09 Minor Features
- Chapter 10 Outlook: Introduction of C++20
- Appendix 1: Further Study Materials
- Appendix 2: Modern C++ Best Practices
Chapter 09 Minor Features
9.1 New Type
long long int
long long int
is not the first to be introduced in C++11.
As early as C99, long long int
has been included in the C standard,
so most compilers already support it.
C++11 now formally incorporate it into the standard library,
specifying a long long int
type with at least 64 bits.
9.2 noexcept
and Its Operations
One of the big advantages of C++ over C is that C++ itself defines a complete set of exception handling mechanisms. However, before C++11, almost no one used to write an exception declaration expression after the function name. Starting from C++11, this mechanism was deprecated, so we will not discuss or introduce the previous mechanism. How to work and how to use it, you should not take the initiative to understand it.
C++11 simplifies exception declarations into two cases:
- The function may throw any exceptions
- The function can't throw any exceptions
And use noexcept
to limit these two behaviors, for example:
void may_throw(); // May throw any exception |
If a function modified with noexcept
is thrown,
the compiler will use std::terminate()
to
immediately terminate the program.
noexcept
can also be used as an operator to manipulate an expression.
When the expression has no exception, it returns true
,
otherwise, it returns false
.
#include <iostream> |
noexcept
can modify the function of blocking exceptions
after modifying a function. If an exception is generated internally,
the external will not trigger. For instance:
try { |
The final output is:
exception captured, from may_throw() |
9.3 Literal
Raw String Literal
In traditional C++, it is very painful to write a string full of
special characters. For example, a string containing HTML ontology
needs to add a large number of escape characters.
For example, a file path on Windows often as: C:\\Path\\To\\File
.
C++11 provides the original string literals,
which can be decorated with R
in front of a string,
and the original string is wrapped in parentheses, for example:
#include <iostream> |
Custom Literal
C++11 introduces the ability to customize literals by overloading the double quotes suffix operator:
// String literal customization must be set to the following parameter list |
Custom literals support four literals:
- Integer literal: When overloading, you must use
unsigned long long
,const char *
, and template literal operator parameters. The former is used in the above code; - Floating-point literals: You must use
long double
,const char *
, and template literals when overloading; - String literals: A parameter table of the form
(const char *, size_t)
must be used; - Character literals: Parameters can only be
char
,wchar_t
,char16_t
,char32_t
.
9.4 Memory Alignment
C++ 11 introduces two new keywords, alignof
and alignas
, to support control of memory alignment.
The alignof
keyword can get a platform-dependent value of type std::size_t
to query the alignment of the platform.
Of course, we are sometimes not satisfied with this, and even want to customize the alignment of the structure. Similarly, C++ 11 introduces alignas
.
To reshape the alignment of a structure. Let's look at two examples:
#include <iostream> |
where std::max_align_t
requires the same alignment for each scalar type, so it has almost no difference in maximum scalars.
In turn, the result on most platforms is long double
, so the alignment requirement for AlignasStorage
we get here is 8 or 16.
Conclusion
Several of the features introduced in this section are those that
use more frequent features from modern C++ features that
have not yet been introduced. noexcept
is the most important feature.
One of its features is to prevent the spread of anomalies,
effective Let the compiler optimize our code to the maximum extent possible.
Changkun Ou © 2016-2024. The book is licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0, code is open sourced under the MIT License.
If you like the book, you could donate the author.