定义 C++运行时函数

发布于 2024-10-16 16:18:44 字数 446 浏览 9 评论 0原文

我正在尝试调整我编写的一些数学代码以允许任意函数,但我似乎只能通过在编译时预先定义它们来做到这一点,这看起来很笨拙。我目前正在使用函数指针,但据我所知,函子也会出现同样的问题。提供一个简单的示例,对于前向差分微分,使用的代码是:

double xsquared(double x) {
    return x*x;
}

double expx(double x) {
    return exp(x);
}

double forward(double x, double h, double (*af)(double)) {
    double answer = (af(x+h)-af(x))/h;

    return answer;  
}

其中前两个函数中的任何一个都可以作为第三个参数传递。然而,我想做的是传递用户输入(在有效的 C++ 中),而不是必须事先设置函数。任何帮助将不胜感激!

I'm trying to adjust some mathematical code I've written to allow for arbitrary functions, but I only seem to be able to do so by pre-defining them at compile time, which seems clunky. I'm currently using function pointers, but as far as I can see the same problem would arise with functors. To provide a simplistic example, for forward-difference differentiation the code used is:

double xsquared(double x) {
    return x*x;
}

double expx(double x) {
    return exp(x);
}

double forward(double x, double h, double (*af)(double)) {
    double answer = (af(x+h)-af(x))/h;

    return answer;  
}

Where either of the first two functions can be passed as the third argument. What I would like to do, however, is pass user input (in valid C++) rather than having to set up the functions beforehand. Any help would be greatly appreciated!

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

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

发布评论

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

评论(8

尐籹人 2024-10-23 16:18:44

从历史上看,您所要求的功能类型在 C++ 中还没有提供。通常的解决方法是嵌入 C++ 以外的语言的解释器(例如 Lua 和 Python 专门设计用于集成到 C/C++ 应用程序中以允许编写脚本),或者使用您的应用程序创建特定于您的应用程序的新语言。自己的解析器、编译器等。但是,这种情况正在改变。

Clang 是一种新的开源编译器,由 Apple 开发,利用 LLVM。 Clang 的设计初衷是不仅可以用作编译器,还可以用作可以嵌入到应用程序中的 C++ 库。我自己还没有尝试过,但您应该能够使用 Clang 做您想做的事情 - 您可以将其链接为库并要求它编译用户输入到应用程序中的代码。

您可以尝试查看ClamAV 团队已经做了什么这样,新的病毒定义就可以用 C 编写

至于其他编译器,我知道 GCC 最近添加了对插件的支持。也许可以利用它来桥接 GCC 和您的应用程序,但由于 GCC 从一开始就不是为用作库而设计的,因此可能会更困难。我不知道有任何其他编译器具有类似的功能。

Historically the kind of functionality you're asking for has not been available in C++. The usual workaround is to embed an interpreter for a language other than C++ (Lua and Python for example are specifically designed for being integrated into C/C++ apps to allow scripting of them), or to create a new language specific to your application with your own parser, compiler, etc. However, that's changing.

Clang is a new open source compiler that's having its development by Apple that leverages LLVM. Clang is designed from the ground up to be usable not only as a compiler but also as a C++ library that you can embed into your applications. I haven't tried it myself, but you should be able to do what you want with Clang -- you'd link it as a library and ask it to compile code your users input into the application.

You might try checking out how the ClamAV team already did this, so that new virus definitions can be written in C.

As for other compilers, I know that GCC recently added support for plugins. It maybe possible to leverage that to bridge GCC and your app, but because GCC wasn't designed for being used as a library from the beginning it might be more difficult. I'm not aware of any other compilers that have a similar ability.

凉宸 2024-10-23 16:18:44

由于 C++ 是一种完全编译的语言,因此除非编写自己的编译器或解释器,否则无法真正将用户输入转换为代码。但在这个例子中,可以为数学公式的领域特定语言构建一个简单的解释器。一切都取决于你想做什么。

As C++ is a fully compiled language, you cannot really transform user input into code unless you write your own compiler or interpreter. But in this example, it can be possible to build a simple interpreter for a Domain Specific Language which would be mathematical formulae. All depends on what you want to do.

极度宠爱 2024-10-23 16:18:44

您始终可以获取用户的输入并通过编译器运行它,然后执行生成的二进制文件。这当然会存在安全风险,因为它们可以执行任何任意代码。

也许更容易的是设计一种极简语言,让用户定义简单的函数,用 C++ 解析它们以执行正确的代码。

You could always take the user's input and run it through your compiler, then executing the resulting binary. This of course would have security risks as they could execute any arbitrary code.

Probably easier is to devise a minimalist language that lets users define simple functions, parsing them in C++ to execute the proper code.

小鸟爱天空丶 2024-10-23 16:18:44

最好的解决方案是使用 lua 或 python 等嵌入式语言来完成此类任务。请参阅选择嵌入式语言以获取建议。

The best solution is to use an embedded language like lua or python for this type of task. See e.g. Selecting An Embedded Language for suggestions.

小…红帽 2024-10-23 16:18:44

您可以使用 tiny C 编译器 作为库 (libtcc)。

它允许您在运行时编译任意代码并加载它,但它仅适用于 C,不适用于 C++。

一般来说,唯一的方法如下:

  • 将代码传递给编译器并创建共享对象或 DLL
  • 加载此共享对象或 DLL
  • 使用此共享对象中的函数。

You may use tiny C compiler as library (libtcc).

It allows you to compile arbitrary code in run-time and load it, but it is only works for C not C++.

Generally the only way is following:

  • Pass the code to compiler and create shared object or DLL
  • Load this Shared object or DLL
  • Use function from this shared object.
莳間冲淡了誓言ζ 2024-10-23 16:18:44

C++ 与 Perl 等其他语言不同,它无法对自身进行运行时解释。

这里唯一的选择是允许用户编译小型共享库,这些库可以在运行时由应用程序动态加载。

C++, unlike some other languages like Perl, isn't capable of doing runtime interpretation of itself.

Your only option here would be to allow the user to compile small shared libraries that could be dynamically-loaded by your application at runtime.

云归处 2024-10-23 16:18:44

那么,您可以做两件事:

  1. 充分利用 boost/C++0x lambda 并在运行时定义函数。

  2. 如果只需要数学公式,像 muParser 这样的库旨在将字符串转换为字节码,这可以被视为在运行时定义函数。

Well, there are two things you can do:

  1. Take full advantage of boost/C++0x lambda's and to define functions at runtime.

  2. If only mathematical formula's are needed, libraries like muParser are designed to turn a string into bytecode, which can be seen as defining a function at runtime.

记忆消瘦 2024-10-23 16:18:44

虽然这看起来像是一个打击,但有很多人为 c++ 和 c 编写了方程解析器和解释器,其中许多是商业的,许多有缺陷,而且都像人群中的面孔一样不同。一个起点是大学生编写 infix 到 postfix 翻译器。其中一些系统使用括号分组,然后将项目放在堆栈上,就像您在旧 HP STL 库中看到的那样。我花了 30 秒,找到了这个:

http://www.speqmath.com /tutorials/expression_parser_cpp/index.html

可能的搜索字符串:“gcc 'equation parser' infix to postfix”

While it seems like a blow off, there are a lot of people out there who have written equation parsers and interpreters for c++ and c, many commercial, many flawed, and all as different as faces in a crowd. One place to start is the college guys writing infix to postfix translators. Some of these systems use paranthetical grouping followed by putting the items on a stack like you would find in the old HP STL library. I spent 30 seconds and found this one:

http://www.speqmath.com/tutorials/expression_parser_cpp/index.html

possible search string:"gcc 'equation parser' infix to postfix"

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