C++ 中文周刊 第102期

2023-02-25 10:52:51 浏览数 (1)

C 中文周刊 第102期

周刊项目地址


资讯

标准委员会动态/ide/编译器信息放在这里

编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-01-04 第183期

本月邮件列表 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/#mailing2023-02

文章

  • Fun with printing tables with std::format and C 20
代码语言:javascript复制
constexpr size_t Rows = 5;
const std::map<std::string, std::array<double, Rows>> productToOrders{
        { "apples", {100, 200, 50.5, 30, 10}},
        { "bananas", {80, 10, 100, 120, 70}},
        { "carrots", {130, 75, 25, 64.5, 128}},
        { "tomatoes", {70, 100, 170, 80, 90}}
};

// print headers:
for (const auto& [key, val] : productsToOrders)
    std::cout << std::setw(10) << key;
std::cout << 'n';

// print values:
for (size_t i = 0; i < NumRows;   i) {
    for (const auto& [key, val] : productsToOrders) {
        std::cout << std::setw(10) << std::fixed 
                  << std::setprecision(2) << val[i];
    }
    std::cout << 'n';
}

template <typename T>
size_t MaxKeyLength(const std::map<std::string, T>& m) {
    size_t maxLen = 0;
    for (const auto& [key, val] : m)
        if (key.length() > maxLen)
            maxLen = key.length();
    return maxLen;
}
const auto ColLength = MaxKeyLength(productsToOrders)   2;

// print values:
for (size_t i = 0; i < NumRows;   i) {
    for (const auto& values : std::views::values(productsToOrders)) {
        std::cout << std::format("{:>{}.2f}", values[i], ColLength);
    }
    std::cout << 'n';
}
/*
****apples***bananas***carrots**tomatoes
    100.00     80.00    130.00     70.00
    200.00     10.00     75.00    100.00
     50.50    100.00     25.00    170.00
     30.00    120.00     64.50     80.00
     10.00     70.00    128.00     90.00
*/

没啥说的

  • Named Booleans prevent C bugs and save you time

if里面的条件判断,最好外面算好再放进if里

代码语言:javascript复制
if ((_someLongNamedVar != FooLongNameEnum::Unknown && _someLongNamedMap.count   (_someLongNamedVar) == 0))

改成

代码语言:javascript复制
bool someLongNamedVarIsNotUnknown = _parameterCommand != FooLongNameEnum::Unknown;
bool someLongNamedMapCountIsZero = _someLongNamedMap.count(_someLongNamedVar) == 0

提高可读性,关爱同事

  • Few lesser known tricks, quirks and features of C

一些c的边角邪门歪道。只有特殊场景能用到,比如位域

代码语言:javascript复制
struct cat {
    unsigned int legs  : 3;  // 3 bits for legs  (0-4 fit in 3 bits)
    unsigned int lives : 4;  // 4 bits for lives (0-9 fit in 4 bits)
};

struct bar {
    unsigned char x : 5;
    unsigned short  : 0; // 帮你padding
    unsigned char y : 7;
}

就不逐一介绍了

  • C Coroutines Part 1: co_yield, co_return and a Prime Sieve
  • Did you know that std::unique_ptr can be constexpr in C 23?
代码语言:javascript复制
struct interface {
    constexpr virtual ~interface() = default;
    constexpr virtual auto get() const -> int = 0;
};

struct implementation final : interface {
    constexpr explicit(true) implementation(int value) : value{value} {}
    constexpr auto get() const -> int { return value; }

private:
    int value{};
};

constexpr auto foo(auto value) {
    std::unique_ptr<interface> i = std::make_unique<implementation>(value);
    return i->get();
}

static_assert(42 == foo(42));

逆天

  • Why am I getting an unhandled exception from my C function that catches all exceptions?
代码语言:javascript复制
void MyClass::DoSomething() {
    try {
        auto name = m_user.GetName();
        m_label.Text(name);
    } catch (...) {
        m_label.Text(L"unknown");
    }
}

如果m_label.Text(L"unknown");异常,怎么办?

一种猥琐的角度

代码语言:javascript复制
winrt::fire_and_forget MyClass::DoSomethingAsync()
{
    auto lifetime = get_strong();
    try {
        auto name = co_await m_user.GetNameAsync();
        m_label.Text(name);
    } catch (...) {
        try {
            m_label.Text(L"unknown");
        } catch (...) {
            LOG_CAUGHT_EXCEPTION();
        }
    }
}

你就说catch没catch住吧,别管几个if

或者,不太常见的写法

代码语言:javascript复制
winrt::fire_and_forget MyClass::DoSomethingAsync() try
{
    auto lifetime = get_strong();
    try {
        auto name = co_await m_user.GetNameAsync();
        m_label.Text(name);
    } catch (...) {
        m_label.Text(L"unknown");
    }
} catch (...) {
    // The function is best-effort. Ignore failures.
}

你学废了吗

  • Float Compression

说实话,我不是很懂。值得研究一波

  • C OpenTelemetry Setup

教你使用opentelemetry

[Daily bit(e) of C

Modern documentation tools](https://itnext.io/daily-bit-e-of-c-modern-documentation-tools-9b96ba283732)

介绍文档工具和github action集成

  • import CMake; C 20 Modules

还是实验性质。感觉没人用

  • Procedural Map Generation in C — Part 1: The slow, the bad and the ugly

图形生成?高游戏的?我不是很懂

  • Behind the magic of magic_enum

讲解magic_enum原理

  • Shaving cycles: faster bucketing

看不懂

视频

  • Reflection without Reflection TS - Fabian Renn Giles - Meeting C 2022

还是__buildin_dump_struct实现。循序渐进。可以看看。我周末传b站一份

  • Smarter Cpp Atomic Smart Pointers - Efficient Concurrent Memory Management - Daniel Anderson CppCon

这个也很有意思,值得研究研究。我周末传b站一份

开源项目需要人手

  • asteria 一个脚本语言,可嵌入,长期找人,希望胖友们帮帮忙,也可以加群384042845和作者对线 最近写了很多高科技,比如https://github.com/lhmouse/asteria/blob/master/rocket/ascii_numget.hpp 很多细节考量
  • pika 一个nosql 存储, redis over rocksdb,非常需要人贡献代码胖友们, 感兴趣的欢迎加群294254078前来对线 有很多的issue task没人做,想参与开源项目开发的,来练练手

新项目介绍/版本更新

  • concurrencpp 又一个协程库 工作招聘

有想打广告的可以发给我。五毛一条


看到这里或许你有建议或者疑问或者指出错误,请留言评论! 多谢! 你的评论非常重要!也可以帮忙点赞收藏转发!多谢支持!

本文永久链接

如果有疑问评论最好在上面链接到评论区里评论,这样方便搜索,微信公众号有点封闭/知乎吞评论

代码语言:txt复制
     This site is open source. [Improve this page](https://github.com/wanghenshui/cppweeklynews/edit/dev/posts/102.md).

0 人点赞