C + + dynamic news push issue 39

article

This time related support should be date library Implementation of. This has been promoting the standard. I don't know how it is going

constexpr auto thanksgiving = November / 25 / 2021y;

static_assert(year(2021) == thanksgiving.year());
static_assert(month(11)  == thanksgiving.month());
static_assert(day(25)    == thanksgiving.day());

You can get a faster speed than gettimeofday, but rdtsc also has a price. Here's a little popular science

As mentioned earlier, try to use string_view instead of const string_view & in addition, there is no difference in msvc. The reason for the implementation of msvc

Is the initialization of the structure of c

struct Point { 
    double x { 0.0 };
    double y { 0.0 };
};

const Point p { .x = 10.0, .y = 20.0 };
 
const Point offset { .x { 100.0 }, .y { -100.0 } };

// mix also possible:
const Point translation { .x = 50.0, .y { -40.0 } };

mt19937 is too slow to implement Xoshiro256ss replace

This paper introduces how to deal with these scenarios in c++20

How to make members generate [[no_unique_address]] + STD:: conditional according to conditions_ t

How to make member functions generate / restrict concept according to conditions

How to make types in a class generate / restrict inheritance STD:: conditional based on conditions_ t

Simple interfaces are combined with compile time tests, constexpr as much as possible, and then static directly_ Assert test

#include <vector>
#include <string_view>
#include <numeric>

// std::isdigit is not constexpr
constexpr bool is_digit(char c)
{
    return c >= '0' && c <= '9';
}

constexpr unsigned int accumulate_string_digits(std::string_view str)
{
    std::vector<unsigned int> digits;
    for (auto c: str)
    {
        if (is_digit(c))
            digits.push_back(c - 48);
    }
    return std::accumulate(digits.begin(), digits.end(), 0);
}
static_assert(accumulate_string_digits("") == 0);
static_assert(accumulate_string_digits("1") == 1);
static_assert(accumulate_string_digits("12345") == 15);
static_assert(accumulate_string_digits("1a23c45c") == 15);
static_assert(accumulate_string_digits("Hello, World!") == 0);

Discuss the abnormal security of the constructor, such as construction errors. The solution is to split the constructor. Remove init and execute it separately, but init may be omitted

Finally, the appropriate solution is to hide the constructor and use create instead of construct generation

class Rectangle {
    // ...

    Rectangle(int w, int h) : width(w), height(h), area(w * h) {
        assert(width > 0 && height > 0);
    }

public:
    static std::optional<Rectangle> create(int w, int h) {
        if (w <= 0 || h <= 0) {
            return std::nullopt;
        }
        return Rectangle(w, h);
    }

    // ...
};

video

Introduce some small features of c++23

string

  • string supports the contains method
  • string constructed from nullptr will directly report an error

Module the pronunciation of module is mom's table. I always pronounce it as Ma Dou

  • Some speed increases

io support

  • std::print kills iostream, which is three times faster than printf
  • Some runtime matching errors in std::format are transferred to the compile time for judgment. / / this is also mentioned in cppcon 2021 and has not been released yet

A bunch of fix and new interfaces in range, and range is more functional

Constexpr various, such as unique_ PTR, cmath interface, optional, etc. memory allocation can even be constexpr

miscellaneous

  • Monadic option makes it easier to use, and_ These little interfaces
  • std::expected is a classic one, in the form of the status class of rocksdb
  • Which library should stacktrace boost
  • byteswap is introduced after std::endian
  • to_underlying gets the value behind the enumeration. You don't need to cast. The standard library tool helps you cast

project

Posted on Sun, 28 Nov 2021 23:13:55 -0500 by sonoton345