10

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?

JoeG
  • 12,994
  • 1
  • 38
  • 63
polapts
  • 5,493
  • 10
  • 37
  • 49
  • 1
    `constexpr` is Turing complete (ignoring the infinity requirements, obviously). – R. Martinho Fernandes Feb 29 '12 at 11:13
  • @R.MartinhoFernandes: If `constexpr` is Turing complete, please use only `constexpr` to model a list of integers growable in both ends. – kennytm Feb 29 '12 at 11:20
  • @R.MartinhoFernandes: You may be interesting in posting that answer to http://stackoverflow.com/questions/9201506/is-constexpr-based-computation-turing-complete. – kennytm Feb 29 '12 at 11:36
  • @R.MartinhoFernandes The proof can be made much simpler. `constexpr` functions are trivially (?) [µ-recursive](http://en.wikipedia.org/wiki/%CE%9C-recursive_function). QED. (Disregarding real-world restrictions, obviously.) – Konrad Rudolph Feb 29 '12 at 12:00
  • After some discussion in the chat I take that back. It might be feasible but it’s not trivial. – Konrad Rudolph Feb 29 '12 at 12:32

1 Answers1

22

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421