为什么使用ADL时``std :: ranges :: size`需要非const方法?

发布于 2025-02-05 15:01:08 字数 1060 浏览 2 评论 0原文

否则,如果范围:: disable_sied_range&lt&std :: remove_cv_t< t>>>是错误的,转换的表达式是有效的,并且具有类似整数的类型, ,其中超载分辨率与以下候选者进行:

  • void size(auto&)= delete;
  • void size(const auto&)= delete; 1
class Test {
    friend size_t size(/*const*/ Test&) {
        return 0;
    }
};

int main() {
    std::ranges::size(Test{});
    // no matching function error when adding the `const` qualifier
}


通常,size方法不需要修改范围,std ::大小

为什么std :: ranges :: size有这样的约束? (似乎它仅针对非成员版本执行。)

Otherwise, size(t) converted to its decayed type, if ranges::disable_sized_range<std::remove_cv_t<T>> is false, and the converted expression is valid and has an integer-like type, where the overload resolution is performed with the following candidates:

  • void size(auto&) = delete;
  • void size(const auto&) = delete;
    1
class Test {
    friend size_t size(/*const*/ Test&) {
        return 0;
    }
};

int main() {
    std::ranges::size(Test{});
    // no matching function error when adding the `const` qualifier
}

https://godbolt.org/z/79e5vrKrT


Generally, size method doesn't require to modify the range, like what std::size does.

Why is there such a constraint of std::ranges::size? (Seems it's only performed for non-member version.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2025-02-12 15:01:08

为什么std :: ranges :: size有这样的约束? (似乎只是
为非会员版本执行。)

尽管size方法没有修改范围,但某些范围没有const qualified成员begin> begin(),仅允许非const Qualified对象来建模range

这也使某些范围适配器在标准中可能只有一个non- const -qualified size

在您的示例中,考虑到test仅具有non- const 开始/endsize size 代码>,然后朋友size_t size(test&amp;)实际上只能是选项。

Why is there such a constraint of std::ranges::size? (Seems it's only
performed for non-member version.)

Although the size method does not modify the range, some ranges do not have a const-qualified member begin(), which allows only non-const-qualified objects to model a range.

This also makes some range adaptors in the standard may only have a non-const-qualified size.

For your example, considering that Test only has non-const begin/end or size, then friend size_t size(Test&) can really only be the option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文