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

Most of this is pretty new to me. Why is it nasty? It looks neat to me!


I consider it nasty (compared to the std::enable_if example that I linked) because it does not express the programmer's intent.

When you use enable_if to enable specific algorithm variants for specific trait predicates, you're saying "here is the specialised algorithm to use when this predicate is satisfied". Both the predicate and the algorithm code that relies on the predicate being satisfied are part of the template specialization definition. There's no chance of missing a precondition: it's stated right in the definition. Another advantage is that it's completely modular -- you can add or remove specializations at will.

On the other hand, the example in the post using 'if constexpr' wraps the algorithm in an extra layer of indirection (a generic wrapper) with a bunch of constexpr if-else spaghetti used to dispatch to the correct implementation. Even if the if-else spaghetti is pristine, the fact remains that the predicate and the specialized code that depends on the predicate as a precondition have been separated out into two independent code sites.




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

Search: