IE盒子

帖子
查看: 92|回复: 1

为什么在C++20中引入std::ssize()

[复制链接]

3

主题

5

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2023-2-23 17:42:19 | 显示全部楼层 |阅读模式
为什么在C++20中引入std::ssize()

std::ssize():
template <class C>
     constexpr auto ssize(const C& c)
         -> std::common_type_t<std::ptrdiff_t,
                               std::make_signed_t<decltype(c.size())>>;

When span was adopted into C++17, it used a signed integer both as an index and a size. Partly this was to allow for the use of "-1" as a sentinel value to indicate a type whose size was not known at compile time. But having an STL container whose size() function returned a signed value was problematic, so P1089 was introduced to "fix" the problem. It received majority support, but not the 2-to-1 margin needed for consensus.
当span被C++17采用时,它使用一个有符号整数作为索引和大小。 部分原因是允许使用“-1”作为标记值,以指示在编译时大小未知的类型。 但是,如果STL容器的size()函数返回一个有符号的值,则会出现问题,因此引入P1089来“修复”这个问题。 它得到了多数支持,但没有达到达成共识所需的2比1的比例。
This paper, P1227, was a proposal to add non-member std::ssize and member ssize() functions. The inclusion of these would make certain code much more straightforward and allow for the avoidance of unwanted unsigned-ness in size computations. The idea was that the resistance to P1089 would decrease if ssize() were made available for all containers, both through std::ssize() and as member functions.
本文P1227建议添加非成员std::ssize和成员ssize()函数。 包含这些元素将使某些代码更加直接,并允许避免在大小计算中出现不需要的无符号。 其想法是,如果ssize()可用于所有容器,无论是通过std::ssize()还是作为成员函数,那么对P1089的阻力都会降低。

可以这么写:
for(int i = 0; i < container.ssize() - 1; ++i)
回复

举报

4

主题

7

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2023-2-23 17:42:40 | 显示全部楼层
我还停留在c++11时代[捂脸]这新标准越来越多了
回复

举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表