r/cpp • • 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

91 comments sorted by

View all comments

Show parent comment

0

u/13steinj 5d ago

That is a much weaker rule than the one that has spontaneously appeared in papers.

For an attribute-token (including an attribute-scoped-token) not specified in this document, the behavior is implementation-defined;

By definition, all attributes mentioned in the standard do not fall under this category. It is perfectly fine to have these contract controls be attributes.

6

u/__christo4us 5d ago edited 5d ago

No, because the standard here also states:

[Note 6: The attributes specified in [dcl.attr] have optional semantics: given a well-formed program, removing all instances of any one of those attributes results in a program whose set of possible executions ([intro.abstract]) for a given input is a subset of those of the original program for the same input, absent implementation-defined guarantees with respect to that attribute. — end note]

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 enforced was 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 with enforced present vs absent (enforced vs possibly ignored evaluations of the predicates of contract assertions and contract violations for certain program inputs).

You could say that enforced could 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:

Our proposed rule for semantic ignorability of standard attributes can be formulated as follows:

Given a well-formed program, removing all instances of a particular standard attribute is allowed to change the observable behaviour of the program, but only if the behaviour with the attribute removed would have been a conforming behaviour for the original program with the attribute present.

We propose that the Second Ignorability Rule be spelled out in the C++ Standard (in [dcl.attr]). Formally, it would only apply to the attributes that are already in the standard, and thus not add any new information per se, as those attributes are already defined to be semantically ignorable in different ways (see discussion in section 3.1.2). However, the existence of such an explicit rule in the standard would be very helpful for codifying the intended behaviour of all future standard attributes, too: any new attribute proposal that does not want to follow the rule would have to carve out an explicit exception for itself. We believe that the presence of the Second Ignorability Rule in the C++ Standard would be a strong enough deterrent for future proposals that they will stick to it, thus leading to consistent language design.

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.