提问人:code muncher 提问时间:8/5/2023 最后编辑:Brian61354270code muncher 更新时间:9/19/2023 访问量:212
std::span 中 std::d ynamic_extent 的目的是什么
What's the purpose of std::dynamic_extent in std::span
问:
我知道是静态的。它只是对一堆向量/数组/等元素的视图。std::span
我看到了 span 的构造函数,它似乎在 4-6 中使用。但是在这些构造函数中,大小 - 需要一个模板参数。对我来说,这意味着 size/count/len 在编译时是已知的。那么到底是什么?std::dynamic_extent
std::size_t N
std::dynamic_extent
答:
4赞
Brian61354270
8/5/2023
#1
is std::d ynamic_extent
的定义是
inline constexpr std::size_t dynamic_extent
= std::numeric_limits<std::size_t>::max();
它是 a 的特殊值,用于指示 具有动态范围。std::size_t
std::span
大小 - 需要一个模板参数。对我来说,这意味着 size/count/len 在编译时是已知的。
std::size_t N
的 “size” 仍然是在编译时指定的,只是当 “size” 采用该特殊值时,它被视为动态范围。std::span
评论
0赞
code muncher
8/7/2023
啊,这是一个令人困惑的 API。感谢
0赞
Jarod42
8/6/2023
#2
构造函数将设置初始大小,但当 span dynamic_extent时,其大小可能会发生变化
std::array<int, 4> arr{1, 2, 3, 4};
std::span<int> s{arr}; // whole array view // size is 4
s = s.subspan(1, 2); // restricted array view // size is 2 now
评论
0赞
code muncher
8/7/2023
在这种情况下,我的新尺寸又有了新尺寸 - 2。如果我有:那么 s2 将static_extent。(我应该试试这个!s
auto s2 = s.subspan(1,2)
0赞
Jarod42
8/7/2023
s.subspan<1, 2>()
将返回静态范围。 返回动态范围。s.subspan(1, 2)
0赞
code muncher
8/7/2023
是的!我现在明白了。
评论
std::span
std::span
std::size_t N