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.

7
  • 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. Commented Jan 10, 2016 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. Commented Jan 10, 2016 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. Commented Jan 10, 2016 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. Commented Jan 10, 2016 at 18:50
  • @NicolBolas maybe what the OP means is that scripting languages are often interpreted, rather than compiled Commented Jan 10, 2016 at 18:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@arainone Well, that's a viable but clumsy way of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.