r/cpp • u/grishavanika • 1d 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?
56
Upvotes
5
u/Wurstinator 18h ago
"aliases will not make it better" -> then what would like to see instead, if not shorter names / less nesting?
9
u/DuranteA 14h ago
This seems perfectly fine to me. If you frequently use it and want a shorter spelling in your codebase you can easily make that happen.
(Note: I have no real skin in the contracts game)
13
u/notadragon34 1d ago
p3400's name lookup is designed exactly to fix this (for standard library labels and for your own). similar to udls, it let's us provide short names for standard labels by adding lookup in a namespace that applies only to assertion control expressions.
some, however, have sworn that such name lookup modifications are omdb issues. we will see where that goes.
as the author, i am sad as well, for many reasons. i hope that c++29 lets us fix most of them.
38
u/Wenir 1d ago
This is embarrassing
27
u/holyblackcat 18h ago
This feels like being done out of spite, to be honest.
"Oh, you're blocking C++26 unless we add this? Wish granted, but the spelling will be half the screen long."
20
u/El_RoviSoft 1d ago
it’s just a normal thing at this point
that’s why headers like fwd.h exist in almost every mature codebase
8
u/no-sig-available 17h ago
This is just an option, you don't have to use it everywhere.
If you compile all your sources with contracts enforced, you don't have to specify it in the source code as well.
17
u/__christo4us 1d ago
I think
enforcedshould have been an attribute. Then the code would have become this:11
u/LiAuTraver 23h ago
How we made a simple stuff extremely verbose and complicated. Very C++, plus very sad.
Still didn't understand why we made contracts this huge for a thing which a plain assert and debug assert would simply suffice.
7
u/sephirostoy 1d ago
std::contracts::enforced probably would have been enough.
3
u/lally 11h ago
Thinking about this, contracts are really necessary right now.
I happily use an LLM to write code for me. Often I ask it to do things it doesn't do very well, so I then ask it to write up the protocol (e.g. lock-free data sharing) in something like a formal state machine, a petri net, or TLA+. But we need a way to make sure the code fits the model. Contracts are very useful for that.
As for all the ::s, we crossed that bridge a long time ago. There are so many ::s in modern C++ code when i write it (at least to use the now-decent standard library). I've learned to let go of that, and accept that namespace aliases are part-and-parcel of using the language. Contracts aren't the cause of it.
3
u/manni66 13h ago
I'm increasingly getting the impression that everyone has a different idea of what contracts are supposed to be. I doubt that bolting on more features as an afterthought will change that.
3
u/Challanger__ 1d ago
formattingin post2
u/Tringi github.com/tringi 22h ago
I'll repeat my ¢2 from the previous discussion. It probably gave some people heart attack, but still: IMHO we need one extra ADL in C++ exactly for these things.
I'll start with a different simple example:
So when we have that,
preandpostautomatically includestd::contracts::controlsin the lookup, and the code becomes:Just an idea.
1
u/samadadi 22h ago
Let me be clear about this atrocity. The standard committee wants me to type this in my code when I am creating a function that needs a runtime check so I became a little bit safer. So basically contracts are just some guardrails that we extract from the body and we put in the function signature and make a ugly and unreadable mess. But what if we do not know those guardrails in the first place as all non-expert CPP programmers do not know most of the time because if they did then they would have put these guardrails in the first place. Man CPP committee is desperate to destroy CPP language by making it more bloated. Imagine being a new programmer and seeing CPP tutorials. Just WoW. Every time I see some of these papers I am getting more convinced to switch to Rust or Zig.
1
u/cristi1990an ++ 16h ago
Yeah, nobody's gonna bother with this
1
u/EmaerSeven 17h ago
Why not simply write a `positive_int` template class that checks conditions in its constructor/methods and, if necessary, throws an exception or triggers an assertion?
-1
u/mcencora 14h ago
I hate it, and the whole 64-pages long paper P3400 that adds contract labels.
So much added complexity...
If the NBs issue is lack of enforced contracts, why not solve it by adding something like this in the standard:
"Program is ill-formed if linked/compiled TUs use incompatible/different contract evaluation semantics."
Then we will get best of both words - those who want to enforce/ignore contracts can compile their program and dependencies the way they like, there will be:
- no mixed mode hence no "not ODR per standard, but actually ODR",
- no "contracts may be evaluated multiple times" issue,
- no "contract evaluation ordering can lead to UB" like this `void f(int *p) pre(p != nullptr) pre(*p != 42);`.
0
u/UndefinedDefined 5h ago
I think it doesn't matter, because nobody is going to write such code. It's too long, too verbose, just use your own assert macro and be done with this.
-2
u/def-pri-pub 11h ago
I haven’t written this much syntax for one feature since learning Java back in 2009.