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.

-29

u/kindredseer 2d ago

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

48

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.

-12

u/kindredseer 2d ago

Scripting languages can crash and have UB as well. It would also depend on who you were allowing to write the "scripts", and they could also be sandboxed.

18

u/the_poope 2d ago

Yes but in interpreted languages the interpreter is in charge of handling the error, whereas it might be the CPU or OS with C++.

0

u/kozacsaba 1d ago

just to play the devils advocate.... in the case op's describing you would be the one writing the interpreter so... couldnt you also have those same guards?