指定合同违规处理程序 [英] Specifying a violation handler for contracts
问题描述
在C ++中支持基于合同的编程b $ b 在Rapperswil的C ++ 20工作草案中被采用。此语言功能的一部分是违反处理程序的概念,当违反合同时将调用该处理程序。
Support for contract based programming in C++ was adopted ino the C++20 working draft in Rapperswil. One part of this language feature is this notion of a violation handler which will be invoked when a contract is violated.
草药萨特的旅行报告指出:
您可以安装自己的违规处理程序并进行运输
You get to install your own violation handler and ship a release build with the option of turning on enforcement at run time.
但是 [dcl.attr.contract] 说:
程序的违规处理程序是
noexcept
opt $ b类型的函数$ b函数(对const std :: contract_violation
的左值引用)返回void
,并在实现定义的方式。 [...]不应采用编程方式来设置或修改违规处理程序。它是由实现定义的,它是如何为程序建立违规处理程序的,以及如何设置std :: contract_violation
([support.contract.cviol])参数的值,
The violation handler of a program is a function of type "
noexcept
opt function of (lvalue reference toconst std::contract_violation
) returningvoid
", and is specified in an implementation-defined manner. [...] There should be no programmatic way of setting or modifying the violation handler. It is implementation-defined how the violation handler is established for a program and how thestd::contract_violation
([support.contract.cviol]) argument value is set, except as specified below.
这对我来说还是很不清楚的。实现如何允许我以非编程方式设置自己的违规处理程序?我必须在gcc,clang和msvc上做什么?
This is very unclear to me. How might an implementation allow me to set my own violation handler, in a non-programmatic way? What will I have to do on gcc, clang, and msvc?
推荐答案
在非编程方式中,实现如何允许我设置自己的违规处理程序
How might an implementation allow me to set my own violation handler, in a non-programmatic way?
这是要确定的实现,但我宁愿怀疑这将是某种命令行参数。您将命名一个函数,编译器/链接器将使它成为违规处理程序。否则,他们可能会选择您要实现的某些特定函数名称。
That's for implementations to determine, but I rather suspect it would be a command line parameter of some sort. You'd name a function, and the compiler/linker would make that the violation handler. And if not that, then they would probably pick some specific function name that you implement.
此处的要点是,所使用的函数是 static >,从C ++抽象模型的角度来看。当编译器启动时,它确切地知道将调用哪个函数,就像系统对 main
的调用以及处理<$ c的程序部分一样$ c> main 的返回值。
The point here is that the function being used is static, from the perspective of the C++ abstract model. When the compiler starts, it knows exactly which function will be called, much like the call by the system to main
, as well as the part of the program that handles main
's return values.
这篇关于指定合同违规处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!