是否可以在 C++ 运行时动态创建函数?

Is it possible to create a function dynamically, during runtime in C++?(是否可以在 C++ 运行时动态创建函数?)
本文介绍了是否可以在 C++ 运行时动态创建函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

C++ 是一种静态的编译语言,模板在编译时解析等等...

C++ is a static, compiled language, templates are resolved during compile time and so on...

但是是否可以在运行时创建一个函数,在源代码中没有描述,在编译过程中也没有转换成机器语言,这样用户就可以向它抛出源中没有预料到的数据?

But is it possible to create a function during runtime, that is not described in the source code and has not been converted to machine language during compilation, so that a user can throw at it data that has not been anticipated in the source?

我知道这不可能以一种直接的方式发生,但肯定是有可能的,有很多编程语言没有被编译,而是动态地创建了用 C 或 C++ 实现的那种东西.

I am aware this cannot happen in a straightforward way, but surely it must be possible, there are plenty of programing languages that are not compiled and create that sort of stuff dynamically that are implemented in either C or C++.

也许如果为所有原始类型创建工厂,以及将它们组织成更复杂的对象(例如用户类型和函数)的合适的数据结构,这是可以实现的吗?

Maybe if factories for all primitive types are created, along with suitable data structures to organize them into more complex objects such as user types and functions, this is achievable?

欢迎提供有关该主题的任何信息以及指向在线材料的提示.谢谢!

Any info on the subject as well as pointers to online materials are welcome. Thanks!

我知道这是可能的,更像是我对实现细节感兴趣:)

I am aware it is possible, it is more like I am interested in implementation details :)

推荐答案

,当然,没有任何工具在其他答案中提到,但只是使用 C++ 编译器.

Yes, of course, without any tools mentioned in the other answers, but simply using the C++ compiler.

只需在您的 C++ 程序中执行这些步骤(在 linux 上,但在其他操作系统上必须类似)

just follow these steps from within your C++ program (on linux, but must be similar on other OS)

  1. 使用 ofstream
  2. 将 C++ 程序写入文件(例如在/tmp/prog.cc 中)
  3. 通过system("c++/tmp/prog.cc -o/tmp/prog.so -shared -fPIC");
  4. 编译程序
  5. 动态加载程序,例如使用 dlopen()

这篇关于是否可以在 C++ 运行时动态创建函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why does C++ compilation take so long?(为什么 C++ 编译需要这么长时间?)
Why is my program slow when looping over exactly 8192 elements?(为什么我的程序在循环 8192 个元素时很慢?)
C++ performance challenge: integer to std::string conversion(C++ 性能挑战:整数到 std::string 的转换)
Fast textfile reading in c++(在 C++ 中快速读取文本文件)
Is it better to use std::memcpy() or std::copy() in terms to performance?(就性能而言,使用 std::memcpy() 或 std::copy() 更好吗?)
Does the C++ standard mandate poor performance for iostreams, or am I just dealing with a poor implementation?(C++ 标准是否要求 iostreams 性能不佳,或者我只是在处理一个糟糕的实现?)