C 中文周刊 第110期
资讯
cppiso有个开发者调查问卷有空闲的没事儿干的可以投一下 https://www.surveymonkey.com/r/isocpp-2023
标准委员会动态/ide/编译器信息放在这里
编译器信息最新动态推荐关注hellogcc公众号 本周更新 2023-04-19 第198期
周六有深圳meetup
ACCU 2023要开始了。去年的没啥意思
视频信息密度低,看起来真的太费时间了
本周内容也不多
文章
- operator<=> for C 20入门篇
老文了,这里列下代码
代码语言: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<=>
再相等情况下的性能不太行
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; // 加上这一行
};
- Functional exception-less error handling with C 23’s optional and expected
直接列代码吧,以前的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);
}
这多干净
- Did you know that C 23 deprecated std::aligned_storage and std::aligned_union?
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)
- Trivial functions can still be non-nothrow (modulo compiler bugs)
看不懂
- Defining interfaces in C with ‘concepts’ (C 20)
- Defining interfaces in C : concepts versus inheritance
老生常谈,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];
}
};
这两种写法,第一种也更清晰
- ELF hash function may overflow
这种场景也挺难触发的
- Kratos-一个基于现代C 的开源有限元框架
不是很懂
- Protecting a broker from a failing event handler
看不懂