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

FTA: if constexpr(Addable<T>){ /.../ } else if constexpr(requires(T a, T b) { a + b; }){ /.../ }

The compiler knows that Addable<T> is a concept at this point right? Is the constexpr required?



Addable<T> should evaluate to bool, so you can actually use it with a plain if.

But a plain if requires both true and false branches to type check and if your T is not actually addable and you use operator+ in the true branch you will get a compilation error.


Aah got it. Thank you!




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

Search: