1

As the title, I am wondering if there is any situation where something can be written in compile-time-if but not template specialization, or vice versa? Or perhaps preference to one over the other?

The only situation I can come up with is where you have a template function with multiple template parameters and you want to have cases (if-else) on one of those parameters. Since you cannot have partial template specialization with function, then you must use compile-time-if (However, you might be able to get around by overloading?).

I am not too sure. So hope some C++ expert out here gives me some insight. In case the answer is no, then why bother to introduce compile-time-if?

jbapple
  • 3,297
  • 1
  • 24
  • 38
Gary
  • 1,828
  • 1
  • 20
  • 27
  • 4
    "*why bother to introduce compile-time-if*" You mean, besides the obvious convenience factor? – Nicol Bolas Oct 31 '21 at 15:36
  • 1
    Are you familiar with tag dispatch? How the standard iterator utilities can do the most efficient things with different iterator categories? There's your *immense* simplification brought on by constexpr if. Consequently it has nothing to do with function template specialisations, which are usually the wrong tool for most jobs. – StoryTeller - Unslander Monica Oct 31 '21 at 16:22
  • 2
    Since the C++11 template machinery was shown to be Turing-complete, I don't believe there's anything that can be done with `if constexpr` that can't also be done with template metaprogramming. `if constexp` exists basically for the same reason high-level programming languages exists, the same reason we don't usually code Turing machines by hand. – Igor Tandetnik Oct 31 '21 at 17:24

0 Answers0