“评估功能”在 C++

发布于 2024-11-08 15:50:33 字数 91 浏览 0 评论 0原文

我想知道 C++ 中是否存在像 Matlab 那样的“评估”函数。

实际上,我需要一个可以像命令行一样解释字符串的函数。

感谢您的回答。

I want to know if exists an "evaluate" function in C++ like the Matlab one.

In practise, I need a function that can interprets a string like a command line.

thanks for the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会傲 2024-11-15 15:50:33

如果您实际上尝试在正在运行的 C++ 应用程序中“评估”C++ 源代码,那么基本上不行——这不是该语言指定的功能。

C++ 子集(例如 CInt、Ch 和 UnderC)有解释器 - 如果您的 C++ 程序相对简单,它们也许能够运行它。或者,某些可以嵌入到已编译的 C++ 程序中,以允许某些运行时源代码评估,但对预编译代码及其变量的访问和更改能力有限。

正在运行的程序也可以调用编译器并动态加载/链接生成的库,但这是一种非常不寻常的做法,并且并非没有性能、安全性和互操作性问题:

  • 为对于编译器来说,编译和链接是一个相对消耗资源且缓慢的操作,但是一旦链接了库,新代码就可以以正常的外线函数调用速度执行,从而
  • 解决了执行外部进程时常见的问题
    • 确保路径和编译器可执行文件名称不会被程序的恶意输入更改
    • 没有恶意软件替代或感染编译器
    • 动态源代码不包含诸如 system()exec()unlink() 调用之类的语句,滥用网络连接、滥用不必要的 CPU/内存/描述符等。
  • 预编译的 C++ 程序可以不能被新链接的代码修改或轻松/深入地探测,因此新行为的主要机制必须已经设计到预编译的应用程序中:对新可访问的变量、函数和工厂方法/虚拟调度的期望。

如果您实际上需要更有限的东西,例如评估数学表达式或逻辑谓词的能力,可能以 C++ 源代码风格表示,也许读取或设置一些值,那么各种更有限和专门的库和嵌入式解释可用。甚至还有用于创建此类解析器的库,例如 boostspirit 库。

最后,其他语言(Lua、Ruby、Python、Perl、TCL 等)的解释器可以嵌入到 C++ 应用程序中,从而实现互操作性和安全性的各种方法。

If you are actually trying to "evaluate" C++ source code within a running C++ application, then basically no - it's not a feature specified by the language.

There are interpreters for subsets of C++ (e.g. CInt, Ch and UnderC) - they may be able to run your C++ program if it's a relatively simple one. Alternatively, some can be embedded within a compiled C++ program to allow some run-time source code evaluation, but with limited access to and ability to change the pre-compiled code and its variables.

It's also possible for a running program to invoke the compiler and dynamically load/link a resultant library, but this is a very unusual practice and not without performance, security and interoperability issues:

  • creating a new process for the compiler, compiling and linking is a relatively resource-hungry and slow operation, but once the library's linked the new code can be executed at normal out-of-line function call speeds
  • the usual issues with executing an external process
    • ensuring the path and compiler executable name can't be changed by malicious inputs to the program
    • that no malware is substituted for or infecting the compiler
    • on-the-fly source code doesn't contain statements like system(), exec(), unlink() calls, abuse network connectivity, chew unwarranted CPU/memory/descriptors etc.
  • the pre-compiled C++ program can't be modified or easily/deeply probed by the newly linked code, so the main mechanisms for new behaviour must have been designed in to the pre-compiled application already: expectations for newly accessible variables, functions, and factory methods / virtual dispatch.

If you actually need something more limited, like the ability to evaluate mathematical expressions or logical predicates, possibly expressed in a C++-source style, perhaps reading or setting some of your values, then various more limited and specialised libraries and embedded interpreted are available. There are even libraries for creating such parsers, such as the boost spirit library.

Finally, interpreters for other languages - Lua, Ruby, Python, Perl, TCL etc. - may be embedded in the C++ application, sporting various approaches to interoperability and security.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文