gpt4 book ai didi

c++ - std::(customization point) 是否调用最合适的重载?

转载 作者:行者123 更新时间:2023-12-01 13:42:36 25 4
gpt4 key购买 nike

从C++20开始,在[namespace.std]/7中引入了定制点的概念。 :

Other than in namespace std or in a namespace within namespace std, a program may provide an overload for any library function template designated as a customization point, provided that (a) the overload's declaration depends on at least one user-defined type and (b) the overload meets the standard library requirements for the customization point. [ Note: This permits a (qualified or unqualified) call to the customization point to invoke the most appropriate overload for the given arguments. — end note ]



注释部分(注意强调词“合格”)是否意味着 std::f将自动为 f 调用最合适的重载如果 std::f是定制点吗?

一个真实的例子是 std::swap ,这是一个指定的定制点。这是否意味着从 C++20 开始,我们可以写 std::swap(a, b)直接代替 using std::swap; swap(a, b); ?

最佳答案

A real example is std::swap, which is a designated customization point. Does this mean since C++20, we can write std::swap(a, b) directly instead of using std::swap; swap(a, b);?



std::swap本身并没有获得任何力量。它仍然只是一个函数模板,所以如果你直接调用它,你就是……直接调用它。没有 ADL 或任何东西。

重点是说应该如何选择自定义点。也就是说,你写:
namespace N { // not std
void swap(Foo&, Foo&);
}

不是:
namespace std {
void swap(N::Foo&, N::Foo&);
}

也不:
namespace std {
template <>
void swap(N::Foo&, N::Foo&);
}

然而,C++20 确实引入了很多新的东西,叫做 customization point objects你可以直接使用它来做这种事情。 swap的CPO拼写为 std::ranges::swap (同样,所有有用的范围都有 CPO... ranges::beginranges::end 等)。

关于c++ - std::(customization point) 是否调用最合适的重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60386991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com