Warning, you probably don’t want to do this
Instead: Please strongly consider creating a chaiscript instance then passing it into each function call that might need to use it.
This is a much better solution, and the option I used in Spiced. This gives you the flexibility to do things like swap out your scripting engine, use multiple instances of the scripting engine, etc.
Also note that if you run into any problems with the below solution I may not be able to support you because they could be specific to your compiler/platform/codebase
Now that I’ve warned you…
Do not even consider using this method if you are using MSVC 2013 or earlier
I’m reluctant to explain how to make a singleton in C++, because there are many varied issues with singletons that can sometimes bite you in unexpected ways.
However, many of these problems were solved with C++11, so I guess I’ll share.
static ChaiScript::chaiscript& get_instance();
static chaiscript::ChaiScript chai;
This method uses the “construct on first use” idiom, which means the static object
chai is constructed and initialized the first time the
get_instance method is called.
Usage would be like you are suggesting.
I guess I’ll say this: I’m warning you that this is probably a bad idea in the long run.
Hopefully that was enough disclaimers…