Specifying a violation handler for contracts

亡梦爱人 提交于 2019-12-10 14:23:30

问题


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.

Herb Sutter's trip report states that:

You get to install your own violation handler and ship a release build with the option of turning on enforcement at run time.

But the wording in [dcl.attr.contract] that this paper added says:

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.

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/51145658/specifying-a-violation-handler-for-contracts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!