logoalt Hacker News

drivebyhooting • today at 3:58 AM • 4 replies • view on HN

Your comment just made me realize contracts is different from concepts (which are basically bounded genetics, right…?)


Replies

jandrewrogers • today at 4:19 AM

C++ concepts evaluate the properties of types at compile-time. You can inject arbitrary constraints on type properties anywhere in the code including conditional codegen. It is a way of asking if a type “can express this complex set of abstract things or has this enumerated set of properties” with almost arbitrary detail. They are extremely powerful in terms of expressiveness and specialization of code. Insanely good for performance and reliability engineering.

One of the best features to be added to C++ in many years.

C++ contracts are an attempt to bring C asserts into the C++ age. The weakness of C++ contracts is that they are mostly syntactic sugar over C asserts. If you really care about this type of thing, you probably already have a custom C++ assertion framework that is at least as capable if not more.

The promise of C++ contracts is that they can aid formal verification, which would be great. As delivered, it can’t do much more than was already possible, which will inhibit adoption.

➕ show 2 replies
stingraycharles • today at 4:03 AM

Concepts are evaluated compile time and make a lot of TMP more elegant. Contracts are runtime check, akin to assertions.

Phrased differently, contracts are to concepts what assert is to static_assert.

➕ show 1 reply
YuechenLi • today at 4:54 AM

A concept is a compile time requirement that bounds templates which are the generics in C++, so you are close.

rramadass • today at 4:14 AM

Concepts are for Types at compile time while Contracts are for Values at runtime.