如何在C/C++中进行更多的预处理
How to preprocess more in C/C++
我很想知道,一些比我知识渊博的人可能会知道答案:
为什么C(在我的例子中是针对C++的(预处理器不更完整?
我的意思是,为什么我们不能使用例如C++作为预处理器语言?它将允许我们做更多关于类的事情,生成动态代码等
理想情况下,我想像预处理器宏一样调用C++函数,举一个具体的例子,我想做一些事情,比如:
#void generateVariable(std::string type, std::string name) {
# if (name[0] == 'p')
# cout << "protected:" << endl << type << " " << name << ";" << endl;
# if (name[0] == 'm')
# cout << "public:" << endl << type << " " << name << ";" << endl;
# std::string prefix = name;
# prefix.erase(2, npos);
# name.erase(0,2);
# name[0] = toupper(name[0]);
# cout << "public:" << endl << type << "get" << name << "() const { return " << prefix+name << "; }" << endl;
#}
这样我就可以打电话给
class A {
generateVariable(static const int, p_age)
}
它会产生
class A {
protected:
static const int p_age;
public:
static const int getAge() const { return p_age; }
}
有没有真正的方法来做这种事情,而不是用脚本语言解析整个文件并重写它?
预处理器实际上可以这样做(尽管我不建议这样做(:
#define generateVariable(__type__, __name__)
protected: __type__ __name__;
public: __type__ get##__name__() { return __name__; }
class A {
generateVariable(static const int, p_age);
}
get##__name__()
将扩展到getp_age()
。。。
相关文章:
- 错误:无效的预处理指令 #i 的意思是 #if?
- C++预处理会生成变量成员、资源库和映射
- 使用预处理指令检查是否包含标头?
- 预处理的 C/C++ 文件是否特定于计算机?
- 使用 GCC 对 C 文件进行部分预处理(不删除 "define" 指令)
- 在 CPLEX 中求解线性规划,无需剪切和预处理
- CPP -D 选项,用于预处理 Fortran 代码
- 错误:粘贴"tmp_UINT"和"+"未提供有效的预处理令牌
- 任务计划程序库的预处理不起作用 - 多定义错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- 使用python预处理后,C++(opencv)中的垫子类型数据与image_to_array相同
- Howo 使用 cl 预处理为 masm 组装生成一个单独的文件
- 我有一个预处理的 C/C++ 源文件 (cacti.i).如何从这个 .i 文件生成可执行二进制文件,以便我可以像 ./
- 如何使用Visual Studio C/C++编译器(cl.exe)来预处理我的objective-C代码
- 是具有预处理前分支实现的结构违反ODR
- 与不完整的Cholesky预处理的共轭梯度返回特征库的意外错误
- Visual Studio C - 无法输出预处理文件
- 在海湾合作委员会中加快宏观预处理的任何方法
- Xmllint未设置,而在路径中找不到XMLLINT;跳过XML预处理
- 如何在预处理时读取文件