Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 constexpr to obsolete template meta-programming?

As I understand it, constexpr is not Turing complete unlike template metaprogramming, so I believe these are not the same. So the question is to what extent does constexpr make template metaprogramming obsolete?

like image 749
polapts Avatar asked Feb 29 '12 11:02

polapts


1 Answers

constexpr is absolutely Turing-complete. Recursion is allowed. It a convenient way to define functions that work at compile time as well as runtime. constexpr functions, being mere functions, cannot perform operations on types, though. (Unless you use template metaprogramming to define said function, of course.)

Both class templates and constexpr can be used to contain compile-time constant expressions, but there the similarity ends. They are not redundant and TMP won't be going away anytime soon.

Some particularly ugly compile-time calculations might be more elegantly rewritten as proper functions, though.

like image 113
Potatoswatter Avatar answered Oct 20 '22 01:10

Potatoswatter