本文介绍了什么是定制点对象以及如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! c ++标准的最新草案介绍了所谓的定制点对象( [customization.point.object] ),被Ranges库广泛使用。The last draft of the c++ standard introduces the so-called "customization point objects" ([customization.point.object]),which are widely used by the ranges library.我似乎理解它们提供了编写自定义版本的开始,交换,数据的方式等等,这是ADL在标准库中找到的。I seem to understand that they provide a way to write custom version of begin, swap, data, and the like, which arefound by the standard library by ADL. Is that correct?与以前的做法不同,在以前的做法中,用户定义了例如开始输入她自己的名称空间的类型?特别是为什么它们 objects ?How is this different from previous practice where a user defines an overload for e.g. begin for her type in her ownnamespace? In particular, why are they objects?推荐答案它们是命名空间 std 中的函数对象实例,可以满足两个目标: first 无条件触发参数的(概念性)类型要求, then 调度到命名空间 std 或通过ADL。They are function object instances in namespace std that fulfill two objectives: first unconditionally trigger (conceptified) type requirements on the argument(s), then dispatch to the correct function in namespace std or via ADL.这是避免第二个查找阶段所必需的,该阶段将直接通过ADL引入用户提供的功能(应按设计推迟 )。详情请参见下文。That's necessary to circumvent a second lookup phase that would directly bring in the user provided function via ADL (this should be postponed by design). See below for more details.在开发应用程序时:您主要不这样做。这是一个标准的库功能,它将为将来的自定义点添加概念检查,希望例如弄乱模板实例时,会收到清晰的错误消息。但是,通过对此类自定义点的限定调用,您可以直接使用它。这是一个符合设计的假想 std :: customization_point 对象的示例:When developing an application: you mainly don't. This is a standard library feature, it will add concept checking to future customization points, hopefully resulting e.g. in clear error messages when you mess up template instantiations. However, with a qualified call to such a customization point, you can directly use it. Here's an example with an imaginary std::customization_point object that adheres to the design:namespace a { struct A {}; // Knows what to do with the argument, but doesn't check type requirements: void customization_point(const A&);}// Does concept checking, then calls a::customization_point via ADL:std::customization_point(a::A{});例如,目前无法使用 std :: swap , std :: begin 等。This is currently not possible with e.g. std::swap, std::begin and the like.让我尝试总结一下本节后面的建议。标准库使用经典定制点有两个问题。Let me try to digest the proposal behind this section in the standard. There are two issues with "classical" customization points used by the standard library. 它们很容易出错。例如,交换通用代码中的对象应该看起来像这样They are easy to get wrong. As an example, swapping objects in generic code is supposed to look like thistemplate<class T> void f(T& t1, T& t2){ using std::swap; swap(t1, t2);}但对 std :: swap进行限定调用(t1,t2)太简单了-永远不会调用用户提供的 swap (请参阅 N4381 ,动机和范围)but making a qualified call to std::swap(t1, t2) instead is too simple - the user-providedswap would never be called (seeN4381, Motivation and Scope)更严重的是,没有办法集中(传递)对传递给此类用户提供的函数的类型的约束(这也是为什么此主题在C ++中变得越来越重要的原因) 20)。同样来自 N4381 的 :More severely, there is no way to centralize (conceptified) constraints on types passed to such user provided functions (this is also why this topic gained importance with C++20). Againfrom N4381:提案中描述的解决方案可以缓解这两个问题b $ b通过以下类似的方法,虚构实现 std :: begin 。The solution that is described in the proposal mitigates both issuesby an approach like the following, imaginary implementation of std::begin.namespace std { namespace __detail { /* Classical definitions of function templates "begin" for raw arrays and ranges... */ struct __begin_fn { /* Call operator template that performs concept checking and * invokes begin(arg). This is the heart of the technique. * Everyting from above is already in the __detail scope, but * ADL is triggered, too. */ }; } /* Thanks to @cpplearner for pointing out that the global function object will be an inline variable: */ inline constexpr __detail::__begin_fn begin{};}首先,对例如 std :: begin(someObject)总是通过 std :: __ detail :: __ begin_fn ,绕行想要的。对于不合格的电话会发生什么,我再次参考原始论文:First, a qualified call to e.g. std::begin(someObject) always detours via std::__detail::__begin_fn,which is desired. For what happens with an unqualified call, I again refer to the original paper:这样,可以在函数对象中执行概念检查。 std 命名空间 之前执行对用户提供的函数的ADL调用。没有办法避免这种情况。This way, concept checking can be performed within the function object in the std namespace,before the ADL call to a user provided function is performed. There is no way to circumvent this. 这篇关于什么是定制点对象以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!