前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 中文周刊 第110期

C++ 中文周刊 第110期

作者头像
王很水
发布2023-04-28 19:35:00
1920
发布2023-04-28 19:35:00
举报

C++ 中文周刊 第110期


资讯

cppiso有个开发者调查问卷有空闲的没事儿干的可以投一下 https://www.surveymonkey.com/r/isocpp-2023

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

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

周六有深圳meetup

ACCU 2023要开始了。去年的没啥意思

视频信息密度低,看起来真的太费时间了

本周内容也不多

文章

老文了,这里列下代码

代码语言:javascript
复制
struct Name {
  string first_name;
  string mid_name;
  string last_name;
};

bool operator<(const Name& other) const {
  //before c++11
  return first_name<other.first_name
      || first_name == other.first_name && mid_name < other.mid_name
      || first_name == other.first_name && mid_name == other.mid_name && last_name < other.last_name;

  //after c++11
  return std::tie(first_name, mid_name, last_name) < 
      std::tie(other.first_name, other.mid_name, other.last_name);
}
// 干净
std::strong_ordering operator<=>(const Name&) const = default;

再比如,控制细节

代码语言:javascript
复制
struct ID {
    int id_number;
    auto operator<=>(const ID&) const = default;
};

struct Person {
    ID id;
    string name;
    string email;
    std::weak_ordering operator<=>(const Person& other) const
    {
        return id<=>other.id;
    }
};

还有就是要注意容器比较,要单独实现operator ==,operator<=>再相等情况下的性能不太行

代码语言:javascript
复制
struct SomeType {
    int int_property;
    std::vector<int> some_ints; // vector是容器
    std::strong_ordering operator<=>(const SomeType&) const = default;
    bool operator==(const SomeType&) const = default; // 加上这一行
};

直接列代码吧,以前的optional就是废物

代码语言:javascript
复制
std::optional<image_view> get_cute_cat (image_view img) {
    auto cropped = find_cat(img);
    if (!cropped) {
      return std::nullopt;
    }

    auto with_tie = add_bow_tie(*cropped);
    if (!with_tie) {
      return std::nullopt;
    }

    auto with_sparkles = make_eyes_sparkle(*with_tie);
    if (!with_sparkles) {
      return std::nullopt;
    }

    return add_rainbow(make_smaller(*with_sparkles));
}

愣是写出的go的味道

代码语言:javascript
复制
std::optional<image_view> get_cute_cat (image_view img) {
    return find_cat(img)
           .and_then(add_bow_tie)
           .and_then(make_eyes_sparkle)
           .transform(make_smaller)
           .transform(add_rainbow);
}

这多干净

代码语言:javascript
复制
template<class T>
struct container {
    //std::aligned_storage_t<sizeof(T), alignof(T)> t_buff; // deprecated
    alignas(T) std::byte t_buff[sizeof(T)]; // okay
};

多用alignas(T)

看不懂

老生常谈,concept比虚接口性能好

代码语言:javascript
复制
template <typename T>
concept is_iterable = requires(T v) {
                        { v.has_next() } -> std::convertible_to<bool>;
                        { v.next() } -> std::same_as<uint32_t>;
                        { v.reset() };
                      };
template <is_iterable T> size_t count(T &t) {
  t.reset();
  size_t count = 0;
  while (t.has_next()) {
    t.next();
    count++;
  }
  return count;
}


class iter_base {
public:
  virtual bool has_next() = 0;
  virtual uint32_t next() = 0;
  virtual void reset() = 0;
  virtual ~iter_base() = default;
};

size_t count_inheritance(iter_base &t) {
  t.reset();
  size_t count = 0;
  while (t.has_next()) {
    t.next();
    count++;
  }
  return count;
}


struct iterable_array : iter_base {
  std::vector<uint32_t> array{};
  size_t index = 0;
  void reset() { index = 0; }
  bool has_next() { return index < array.size(); }
  uint32_t next() {
    index++;
    return array[index - 1];
  }
};

这两种写法,第一种也更清晰

这种场景也挺难触发的

不是很懂

看不懂


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C++ 中文周刊 第110期
    • 资讯
      • 文章
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档