3

Am wondering if there is any "successful" attempts to create a complete set of templates in C++, so, C++ can be used as a scripting language. In other words, get the results at compile time (computation, loop,...) without running the exe.

Think one can calculate recursive items through templates, but having a complete set of functionnality (for, while, and double calculation), I could not find it.

  • `constexpr` goes a long way if your goal is compile-time calculation with familiar code. Templates are Turing-complete, so things like loops can be done a different way. If you're actually after a REPL, I think it's Cling that does that. – chris Jan 10 '16 at 18:44
  • It's effectively functional, so no iterative loops - just recursion. People have done crazy things with it, but only as a technical exercise. My advice is to stay away from template metaprogramming. – RJinman Jan 10 '16 at 18:45
  • 2
    @RJinman, I would say more than a technical exercise. Most Math libraries use heavy expression templates for performance/optimization reasons. Libraries such as Boost.Spirit provide what I find to be a very expressive parser creator without needing to add another build step, not to mention the created parsers being very efficient. – chris Jan 10 '16 at 18:47
  • 8
    I fail to see how pure compile-time execution qualifies as a scripting language. It certainly wouldn't have most of the features of one. – Nicol Bolas Jan 10 '16 at 18:50
  • @NicolBolas maybe what the OP means is that scripting languages are often interpreted, rather than compiled – arainone Jan 10 '16 at 18:54
  • There is still the possibility to run a (c++) application as part of your build process to precalculate values or generate code, which offers much more flexibility then templates. – Naios Jan 10 '16 at 20:27
  • Loop seems also limited by the amount of recursion level with templates.... –  Jan 10 '16 at 22:58

1 Answers1

2

"... get the results at compile time (computation, loop,...) without running the exe."

Nope, there's no way.

Even if everything is computed at compile time, you'll need to run the executable to retrieve the result.

Retrieving the result is not available as a compile time feature.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 5
    Yes you can, although if it just to compile a factorial, you can really have the compiler tell you the result without running the exe, see [this post](http://stackoverflow.com/questions/4977715/calculating-and-printing-factorial-at-compile-time-in-c) – arainone Jan 10 '16 at 18:47
  • 3
    @arainone Well, that's a viable but clumsy way of course. – πάντα ῥεῖ Jan 10 '16 at 18:49