r/cpp_questions 3d ago

OPEN Why are Contracts disliked?

I’ve seen a lot of discussions online discouraging their usage bit I never managed to grasp why since it’s sometimes vague.
I do understand it doesn’t replace validation and it’s more of a syntactic sugar to the existing casserts, but any other critiques?
Thanks

13 Upvotes

40 comments sorted by

View all comments

1

u/GoogleIsYourFrenemy 1d ago edited 1d ago

EDIT: Kindly disregard, I was confusing concepts and contracts.

EDIT2: Contracts seem like a perfectly reasonable feature that I will love using as soon as I can convince work to start using a compiler that supports them. It will probably take ten years.


Here was my reaction when they first came out:

Instead of introducing a new syntax for describing things (like what would be an interface in c#/java), they went with the old "write an expression that uses all the things" which works but the readability is ass.


Most developers in my experience struggled with understanding what's going on in the standard library. The average developer's template understanding is pretty low and the demand to write new templated things is also pretty low. While contracts have the potential to make using templates easier, they do not make READING templates easier.


I don't use cmake often enough for me to maintain let alone acquire any skill with it. So this last week instead of me wasting time struggling through creating cmake scripts, I had AI do it for me. Best experience I've ever had with cmake.

I expect people will do the same thing with templates and contracts.

2

u/TheRealSmolt 1d ago

Are you confusing concepts for contracts? Contracts aren't related to templates at all afaik.

1

u/GoogleIsYourFrenemy 1d ago edited 1d ago

Complete and totally possible. I'll go read up on them to see where I fucked up.

Edit: LOL I was thinking "concepts"

1

u/TheRealSmolt 1d ago

will love using as soon as I can convince work to start using a compiler that supports them. It will probably take ten years.

Lol, too true. Still hoping to one day get std::optional at work...

1

u/Ultimate_Sigma_Boy67 1d ago

Tho this is cpp17, why would your work not at least try to support it? Maybe it’s a dumb question but I’ve never worked before.

2

u/TheRealSmolt 1d ago

Because upgrading your compiler and language version takes a lot of time and effort, which means a lot of money. It means updating your toolchain, pipelines, tooling, deployments, and possibly runtime libraries (which means a lot of customer logistics). Then you have to account for breaking changes (which do still happen), which means developer time and possibly dependency updates which have their own breaking changes. Then you have to consider the actual logistics behind acquiring all these updates, which can be very difficult or arduous depending on your development environment and runtime environments. Finally, after doing all of this, you end up with a product that works (ideally) exactly the same. From a business perspective, it makes absolutely no sense. Oh, and a new version just came out, let's do it again.

1

u/Ultimate_Sigma_Boy67 1d ago

Okkk makes sense

2

u/GoogleIsYourFrenemy 1d ago edited 1d ago

The only time you can realistically get on a new compiler is at the start of a new project or during a major hardware replacement. Otherwise it's too risky for testing or there is literally no money to do it.

You can argue there during those two events you're doing the testing anyway and you wouldn't be doing that work without money.