C++;卸载锅炉板C和x2B的程序+; 我有一个C++程序,它把一个C++程序转储出来。一些函数是锅炉板代码,某些函数具有锅炉板代码和基于几个变量定制的代码

C++;卸载锅炉板C和x2B的程序+; 我有一个C++程序,它把一个C++程序转储出来。一些函数是锅炉板代码,某些函数具有锅炉板代码和基于几个变量定制的代码,c++,C++,下面给出了一个简化示例: // Snippet: 1 #include <fstream> using namespace std; int main() { ofstream fout("output.cc"); // bp() has just boiler plate code fout << "void bp() {" << endl; fout << "std::cout << \"Hell

下面给出了一个简化示例:

// Snippet: 1
#include <fstream>

using namespace std;
int main()
{
    ofstream fout("output.cc");

    // bp() has just boiler plate code
    fout << "void bp() {" << endl;
    fout << "std::cout << \"Hello World!\" << std::endl" << endl;
    // a few hundred lines of C++ code send to fout
    fout << "}" << endl;

    // mix() has boiler plate + some custom code
    int size = 4096;
    fout << "void mix() {" << endl;
    fout << "char buffer[" << size << "];" << endl;
    // a few hundred lines of C++ code send to fout
    fout << "}" << endl;

    // compile output.cc into *.so and delete output.cc

    return 0;
}
然后主文件可以是

intmain()
{
标准::ifstream src(“bp.cc”);
std::流dst(“output.cc”);

dst您可以使用原始字符串文本,只需将代码放入其中一个:

#include <iostream>

char const source[] = R"end(
#include <iostream>
int main() {
    std::cout << "hello, world\n";
}
)end";

int main()
{
    std::cout << source;
}
#包括
字符常量源[]=R“结束(
#包括
int main(){

Std::CUT可能需要有一个类似于资源编译器的东西,它可以用可执行文件捆绑在文件中。另一个选项可能是有另一个小工具,它产生了所有原始代码模板的“可怕”的逃逸语法,并输出一个可编译的C++文件,它具有某些文字(例如:代码>静态conchar)[]
)用于模板。@πάνταῥεῖ 将此作为一个答案发布?哦,没关系-这是和@anatolyg谢谢你提供的链接。我真的不想回答这个问题,这里只有一些指针…@anatolyg,这是一个双重副本吗?我认为这是这个问题最简洁的答案。我们还没有升级到C++11。对于旧版本有解决方案吗?我们确实可以使用Boost。
int main()
{
    std::ifstream  src("bp.cc");
    std::ofstream  dst("output.cc");

    dst << src.rdbuf();
}
#include <iostream>

char const source[] = R"end(
#include <iostream>
int main() {
    std::cout << "hello, world\n";
}
)end";

int main()
{
    std::cout << source;
}