Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So the requires statement just helps the class enforce certain operations?

Like for instance, the class Button would need to implement the void Start(); function?



I think requires/concepts do a few things:

- document an interface (without making you use virtual fns or any weird tricks)

- allow you to use SFINAE without more weird tricks

So yeah, this amounts to enforcing that your class can do certain things... but that’s hugely helpful!


I see it like this: requires can enforce any constraints (any logical predicate/boolean expression) that are able to be checked at compilation time / template instantiation time. So, you can use requires to check other stuff than if certain operations exist, like e.g.:

* that a certain non-type template parameter belongs to a set of hard-coded values

* that a certain non-type template parameter is an even number

* constrain the number of elements in a parameter pack

* things involving sizeof

* that a certain non-type template parameter has a popcount of one (i.e., it only has a single bit)

* that a certain type template parameter is integral AND unsigned

* ...


Not exactly - it was already enforced in C++98. The problem the enforcement was thousands of lines of compile error messages none which said anything remotely like "you didn't implement a Start function". With concepts you skip those lines that need an expert to read (and even they take a while to figure out what the error is really trying to say), and just put in a clear message.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: