さっそくですが
TL でこんなのを目にしました。
誰かC++にeval入れてください
おーけい、やりましょう。
#include <iostream> #include <fstream> #include <stdlib.h> using namespace std; int eval( string const & out_of_main, string const & return_statement ) { ofstream o( "a.cpp" ); o << out_of_main << endl << "int main() { return ( " << return_statement << " ); }" << endl; o.close(); system( "gcc a.cpp -lstdc++" ); // ここでおもむろに gcc return system( "a.exe" ); } int eval( string const & return_statement ) { eval( "", return_statement ); } int main() { cout << eval( "#include <string> \n" // プリプロセッサが使える! "#include <boost/algorithm/string/trim.hpp> \n" " \n" "// trim した文字列長を返す。 \n" // コメントが書ける!( eval なのにリッチ! ) "int len( std::string s ) { \n" // 関数を定義できる! " boost::algorithm::trim( s ); \n" // boost も使える! " return s.length(); \n" "} \n", "len( \" abc \" ) " // 定義した関数を呼ぼう!( クオートに注意! ) ) << endl; }
出力結果: 3