r/cpp • u/grishavanika • 6d ago
std::contracts::controls::enforced
From P4451R2,
#include <contracts>
int f(int x)
pre<std::contracts::controls::enforced>(x > 0)
post<std::contracts::controls::enforced>(r : r > 0)
{
contract_assert<std::contracts::controls::enforced>(x > 0);
return x;
}
Long, 3 levels deep namespace? And
Because the label is found by ordinary name lookup, a namespace alias or a using-directive works
as well:
namespace scl = std::contracts::controls;
Same issue as with C++11 UDL: inconvinient to use, especially in headers, since include + alias is required - aliases will not make it better, in my opinion.
That makes me sad. Am I the only one?
68
Upvotes
View all comments
Show parent comment
6
u/__christo4us 5d ago edited 5d ago
No, because the standard here also states:
It states that all the standard attributes (specified in [dcl.attr]) are semantically ignorable (simply by virtue of each of them having their own specified optional semantics). This means that if
enforcedwas made to be an attribute it would not satisfy this statement since its semantic would not be optional and the observed behavior of the program would differ withenforcedpresent vs absent (enforced vs possibly ignored evaluations of the predicates of contract assertions and contract violations for certain program inputs).You could say that
enforcedcould be added as an exception from this rule but the paper that clarified the ignorability rules in the standard (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2552r3.pdf) states the following:This paper clearly shows the direction of the language design with respect to attributes. Semantic ignorability of attributes is clarified to be a guideline for future standardization.