Specifying a violation handler for contracts


Specifying a violation handler for contracts



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.


noexcept


const std​::​contract_­violation


void


std​::​contract_­violation



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 Answer
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.


main


main





This seems fundamentally weird to me... I guess we'll just have to wait and see when it's implemented?
– Barry
Jul 3 at 13:53





@Barry: It's not that weird. The point is that the function call is static, unlike the call to terminate handlers being dynamic. That way, compilers can optimize it more effectively.
– Nicol Bolas
Jul 3 at 13:54



terminate





I don't think it's remotely important to optimize the violation handler call. Like - that's not even something that should happen in a correct program right? Seems more important to make it easy to specify across multiple compilers.
– Barry
Jul 3 at 13:56





@Barry: I don't think it's a matter of performance optimization of the call itself. It's more a matter of code generation for the testing of contract violation. With this way, it's all static. There may also be DLL/SO issues involved.
– Nicol Bolas
Jul 3 at 13:58






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages