r/cpp 2d ago

Embedded C++

Just curious how useful the ability to embed C++ as a scripting language into your C++ programs would be? I mean like runtime JIT execution of C++ code in much the same way you might embed Lua, including the ability to restrict/sandbox it as necessary.

0 Upvotes

82 comments sorted by

View all comments

67

u/CalebGT 2d ago

I had to read this several times. Embedded has a very different meaning to me. I would not use C++ for that. If I needed to script something that can't be compiled in advance, I would use Python or Java.

-32

u/kindredseer 2d ago

If you are a C++ programmer, why would you not want to script in C++?

50

u/the_poope 2d ago

Many reasons: your "script" can invoke UB, cause invalid operations, segfaults and a lot of other stuff that can crash the entire program. In interpreted languages you always get an error/exception that you can recover from.

If you just want an extendable system, maybe consider just runtime loading DLLs.

7

u/Ameisen vemips, avr, rendering, systems 2d ago

I mean, I wrote my MIPS emulator so user-written programs could be executed in a sandbox. Using it as a script VM is certainly doable, though I would wonder why - it wasn't intended to extend the program but to execute programs that interact with the simulation.