c – 为合同指定违规处理程序

拉普斯维尔的C 20工作草案采纳了
Support for contract based programming in C++
.此语言功能的一部分是违反处理程序的概念,当违反合同时将调用该处理程序.

Herb Sutter’s trip report指出:

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]的措辞是:

The violation handler of a program is a function of type “noexceptopt
function of (lvalue reference to const std​::​contract_­violation) returning void”, 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 the std​::​contract_­violation ([support.contract.cviol]) argument value is set, except as specified below.

这对我来说非常不清楚.实现如何允许我以非编程方式设置自己的违规处理程序?我将在gcc,clang和msvc上做些什么?

最佳答案

How might an implementation allow me to set my own violation handler, in a non-programmatic way?

这是为了确定实现,但我更怀疑它将是某种命令行参数.您将命名一个函数,编译器/链接器将使其成为违规处理程序.如果不是这样,那么他们可能会选择您实现的某个特定功能名称.

这里的要点是从C抽象模型的角度来看,所使用的函数是静态的.当编译器启动时,它确切地知道将调用哪个函数,就像系统对main的调用一样,以及处理main的返回值的程序部分.

点赞