Use case: embed chaiscript in C++ source code


Hello everyone,

I have been using chaiscript, not heavily for now, for a project that must work in iOS/Android.

I wonder if it would be a good idea to make the API constexpr with the following goals:

  • I want to embed my run-time scripts in the source code itself. This can be done, but they will be executed at run-time.

  • It would be nice to get a piece of chaiscript code to be optimized, and, through a constexpr
    string, be able to manipulate and emit c++ to be run directly, at compile-time.

My questions are:

1.- How difficult it would be to constexpr the evaluation API?
2.- Once constexpr API would be in place, how difficult is to emit C++ source code out of chaiscript source code? I see this is as a very appealing feature (even beyond Chaiscript).

My main use case is to develop and later embed the scripts in the final .exe, but I also wonder if it is worthy to generate code from compile-time strings themselves: this would save the translation to C++ in the case there is something to optimize – Just copy/paste the string, the compiler would do the rest through some metaprogramming.


First - I’m glad to hear that you are having some success using ChaiScript on mobile platforms.

I’m pretty sure it’s impossible to use constexpr for evaluation. There are two many unknowns and dynamic cases.

Using constexpr for the parser might be maybe theoretically possible, but it would require a significant rearchitecting away from virtual functions and dynamic allocations that are currently used.

In general I don’t think what you are asking for is possible.

Something to consider however:

If you know what code you want to be static at compile time, you can just make that into a C++ lambda function and pass it directly into ChaiScript. You then kind of get everything you are asking for:

chai.eval("def dosomething(int x, float y) { return x * y + 42; }");


chai.add(fun([](int x, float y) { return x * y + 42; }), "dosomething");

This obviously have some limitations, but so would anything that might be theoretically possible with constexpr.



Well, actually my use case was to be able to directly paste already written code in chaiscript (or another language later) and be able to transform it into C++.

What you say is possible and sensible. It is just I have been thinking of similar experiments for a while.


I asked around on twitter if anyone had any ideas how you might parse and execute script at constexpr compile time and didn’t get any response. If you ever find anything related to this, let us know.


Well, I do not have a concrete idea right now, but for reference: — compile-time strings

Look for metaparse library, I cannot post more than two links in a reply, I am new.

I would try to follow a metaprogramming style more similar to Boost.Hana, but I do not know if you are willing to use c++14 (not 11) in your code-base.

The idea here is to parse a string and generate the API calls (from your already existing APIs, maybe).

So the task would be:

  1. Make your parser purely compile-time. I am not sure if only constexpr is enough, because you would need to generate new types based on the strings, not just parse at compile-time. I think this can be done, just take a look at how Boost.Hana does similar things.

  2. With the string as input, generate API calls to your already existing API (this part does not need to be constexpr-API calls).

This way you would effectively generate c++ code from chaiscript code.

To the question:

  1. If I need to optimize in chaiscript scripts that are slow, what should I do? The pleasant reply would be: just copy/paste your chaiscript code and constexpr evaluate it and let the library handle the rest.

My use case is development with scripts, but package the scripts in the final executable. This can already be done as chaiscript strings that are executed at run-time, but if it were compiled to C++ that would be ideal, avoiding to rewrite in chaiscript scripts in C++, reusing the effort from others. The code would be fast.


I’m actually limited to a subset of C++11 right now because of support of MSVC2013. After 2015 takes off I’ll consider dropping 2013 and moving forward.

I’ll look over the links you shared.

One thing to consider is that ChaiScript is very similar to C++ in syntax and structure, so it may be possible to write a tool that automatically converts ChaiScript into C++, so you could prototype in ChaiScript then convert to C++ at code shipping time.

I don’t know if that would actually make sense or be practical, but I think it’s possible.