r/cpp • • Mar 27 '26

C++26: A User-Friendly assert() macro

https://www.sandordargo.com/blog/2026/03/25/cpp26-user-friendly-assert
53 Upvotes

27 comments sorted by

View all comments

Show parent comment

17

u/LucHermitte Mar 27 '26

I read it as: "we don't modify code that works as soon as a new and better feature appears.". The old way will continue to exist in old code bases.

5

u/EC36339 Mar 27 '26

Well, that's fine.

In that case, SFINAE was just not a good example, because SFINAE, even with standard wrappers like std::enable_if_t is just horrible and unreadable, to the point that you think 3 times whether you use it or just not have type constraints when they are not strictly needed for overload resolution.

3

u/38thTimesACharm Mar 28 '26

SFINAE is the only C++ "feature" where if it's the only way to accomplish something, I strongly consider just not doing that thing.

2

u/EC36339 Mar 28 '26

No. It used to have its justification, but now type constraints can be achieved in better ways.

2

u/TheoreticalDumbass :illuminati: Mar 27 '26

imo transition from assert to p2900 contracts is rather easy, #define assert(...) contract_assert(__VA_ARGS__) , or a sed -i

6

u/pdimov2 Mar 27 '26

This almost works, but assert is an expression, while contract_assert is a statement.

We'll hopefully be able to fix that for C++29 (assuming contracts ship in C++26 at all; there are still people trying to torpedo them.)

10

u/James20k P2005R0 Mar 27 '26 edited Mar 27 '26

This is one of the reasons why I dislike the current state of contracts: if you do this you will run into unexpected undefined behaviour. Contracts do not make the same guarantees as asserts by design, and doing this is a great way to introduce security vulnerabilities into your code

Contract conversion has to be carefully evaluated - you absolutely cannot swap assert for contract_assert