为什么使用ADL时``std :: ranges :: size`需要非const方法?
否则,如果范围:: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管
size
方法没有修改范围,但某些范围没有const
qualified成员begin> begin(),仅允许非
const
Qualified对象来建模range
。这也使某些范围适配器在标准中可能只有一个non-
const
-qualifiedsize
。在您的示例中,考虑到
test
仅具有non-const
开始
/end
或size
size 代码>,然后朋友size_t size(test&amp;)
实际上只能是选项。Although the
size
method does not modify the range, some ranges do not have aconst
-qualified memberbegin()
, which allows only non-const
-qualified objects to model arange
.This also makes some range adaptors in the standard may only have a non-
const
-qualifiedsize
.For your example, considering that
Test
only has non-const
begin
/end
orsize
, thenfriend size_t size(Test&)
can really only be the option.