C++ 动态新闻推送 第61期

2022-05-10 19:28:25 浏览数 (1)

C 动态新闻推送 第61期

资讯

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

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

GCC 12.1 Released

文章

  • Did you know that C 23 added bind_back to simplify writing higher order functions?
代码语言:javascript复制
#define FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)

[[nodiscard]] constexpr auto bind_back(const auto& func, auto... bound_args) {
    return [=] [[nodiscard]] (auto&&... unbound_args) {
        return func(FWD(unbound_args)..., bound_args...);
    };
}

int main() {
  //std::cout << std::bind_front(std::divides{}, 2.)(1.); // prints 2
  std::cout << std::bind_back (std::divides{}, 2.)(1.); // prints 0.5
}
  • C 20 Ranges: The Key Advantage - Algorithm Composition

range代码更干净

代码语言:javascript复制
#include <algorithm>
#include <vector>
#include <iostream>
#include <ranges>   // new header!

int main() {
    const std::vector numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    auto even = [](int i) { return 0 == i % 2; };
 
    std::ranges::reverse_view rv{ 
        std::ranges::drop_view { 
            std::ranges::filter_view{ numbers, even }, 1 
        }
    };
    for (auto& i : rv)
        std::cout << i << ' ';;                                 
}
  • How do I decode a #pragma detect_mismatch error?

一般都是多版本不兼容的问题

  • How can I force a WriteFile or ReadFile to complete synchronously or hang, in order to test something?

windows环境,代码我就不贴了

  • Comparing Memory Sanitizers

对比各种测试工具查内存报错,测试报告 valgrind效果非常好

  • Unpatched DNS Related Vulnerability Affects a Wide Range of IoT Devices

uClibc有bug,尽快升级。否则会被DNS污染

  • Make declaration order layout mandated

介绍std::is_standard_layout

代码语言:javascript复制
#include <iostream>
#include <type_traits>

class A {
  int a;
  int b;
};


class C {
  C (int& ib) : b(ib) {}
  int a;
  int& b;
};


int main() {
  std::cout << std::boolalpha;
  std::cout << std::is_standard_layout_v<A> << 'n';
  std::cout << std::is_standard_layout_v<C> << 'n';
}
  • Producing an empty Windows Runtime type in C /WinRT
代码语言:javascript复制
template<typename T>
constexpr T winrt_empty_value() noexcept
{
    if constexpr (std::is_base_of_v<winrt::Windows::Foundation::IUnknown, T>) {
        return nullptr;
    } else {
        return {};
    }
}
  • The case of the failed exchange of the vtable slot

Virtual­Protect使用问题

视频

  • C Weekly - Ep 322 - Top 4 Places To Never Use const

不要用const T做返回值的声明,会破坏move

代码语言:javascript复制
struct S;
const S foo(); // bad

返回临时变量,临时变量不要用 const T,会破坏copy elision

代码语言:javascript复制
struct S;
S foo() {
  const S s1;
  const S s2;
  if (/*some condition */) {
  	return s1;
  } else {
  	return s2;
  }
}

如果要在传进来的参数上改动返回,没必要const T

代码语言:javascript复制
struct S;
S foo(const S s) {
	// do sth with s
  return s;
}

以及不要const 成员,先确定你的类是不是需要拷贝/move,如果是单例模式没啥问题,如果需要,别const成员

代码语言:javascript复制
struct S {
 const int a;
}

新项目介绍/版本更新

  • Spock SLAF is a Shared Library Application Firewall “SLAF”. 保护使用openssl的应用?
  • anchors 类似timely dataflow的东西,不过写的非常简单
  • catch2 3.0发布

本文永久链接

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

0 人点赞