r/cpp • u/grishavanika • 5d 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
5
u/Wurstinator 5d ago
"aliases will not make it better" -> then what would like to see instead, if not shorter names / less nesting?
16
u/notadragon34 5d 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.
37
u/holyblackcat 5d 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."
44
u/Wenir 5d ago
This is embarrassing
10
u/DuranteA 5d 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)
20
u/El_RoviSoft 5d 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 5d 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.
18
u/__christo4us 5d ago
I think
enforcedshould have been an attribute. Then the code would have become this:3
u/just-comic 4d ago
It's probably not meant to actually be used, was just added to satisfy some of the ISO national bodies.
Contracts are meant to be a debugging aid, not an error handling mechanism. Even Eiffel has ways to disable the contracts at run-time.
12
u/LiAuTraver 5d 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 5d ago
std::contracts::enforced probably would have been enough.
5
u/manni66 5d 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/Tringi github.com/tringi 5d 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.
3
u/lally 5d 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/Challanger__ 5d ago
formattingin post1
u/samadadi 5d 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 ++ 5d ago
Yeah, nobody's gonna bother with this
1
u/Sharp_Performer_8456 3d ago
It's plain ugly. Why not pre!, post! instead?
1
u/EmaerSeven 5d 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 5d 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);`.
-1
u/UndefinedDefined 4d 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.
0
u/megayippie 3d ago
I have asked this elsewhere without answers. Is this now the only language feature that is templated? Can we get
for<par>(...)going forward because we now allow templated language features?-2
u/def-pri-pub 5d ago
I haven’t written this much syntax for one feature since learning Java back in 2009.
-2
u/kokalikesboba 3d ago
What standard is this part of???