【发布时间】:2014-04-08 05:41:30
【问题描述】:
例如,假设我们有这样一个字符串:
string x = "for(int i = 0; i < 10; i++){cout << \"Hello World!\n\";}"
完成以下函数定义的最简单方法是什么:
void do_code(string x); /* given x that's valid c++ code, executes that code as if it were written inside of the function body */
【问题讨论】:
-
编译器一直在这样做。但它确实需要编译器才能正确完成。
-
与其他语言(如 python、ruby 或 javascript)不同,这在 C++ 中并不简单。
-
我猜这可以在 C++ 中的一行中完成。
-
这个问题的“正确”答案可能是“不要那样做”。
标签: c++