Chapter 09 Minor Features

    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:

    1. The function may throw any exceptions
    2. The function can’t throw any exceptions

    And use noexcept to limit these two behaviors, for example:

    If a function modified with noexcept is thrown, the compiler will use std::terminate() to immediately terminate the program.

    1. #include <iostream>
    2. void may_throw() {
    3. throw true;
    4. }
    5. auto non_block_throw = []{
    6. may_throw();
    7. };
    8. return;
    9. }
    10. auto block_throw = []() noexcept {
    11. };
    12. int main()
    13. {
    14. std::cout << std::boolalpha
    15. << "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
    16. << "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
    17. << "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
    18. << "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
    19. return 0;
    20. }

    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:

    The final output is:

    1. exception captured, from my_throw()
    2. exception captured, from non_block_throw()

    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:

    1. // String literal customization must be set to the following parameter list
    2. return std::string(wow1)+"woooooooooow, amazing";
    3. }
    4. std::string operator"" _wow2 (unsigned long long i) {
    5. return std::to_string(i)+"woooooooooow, amazing";
    6. }
    7. auto str = "abc"_wow1;
    8. auto num = 1_wow2;
    9. std::cout << str << std::endl;
    10. std::cout << num << std::endl;
    11. return 0;
    12. }

    Custom literals support four literals:

    1. 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;
    2. Floating-point literals: You must use long double, const char *, and template literals when overloading;
    3. String literals: A parameter table of the form (const char *, size_t) must be used;
    4. 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:

    where std::max_align_t requires exactly 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.

    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.

    Licenses


    This work was written by Ou Changkun and licensed under a . The code of this repository is open sourced under the MIT license.