【发布时间】:2010-05-09 10:52:08
【问题描述】:
我有一个使用 C 野牛解析器的 C++ 项目。 C 解析器使用函数指针结构来调用函数,当产生式被野牛减少时,这些函数会创建适当的 AST 节点:
typedef void Node;
struct Actions {
Node *(*newIntLit)(int val);
Node *(*newAsgnExpr)(Node *left, Node *right);
/* ... */
};
现在,在项目的 C++ 部分,我填写了这些指针
class AstNode {
/* ... */
};
class IntLit : public AstNode {
/* ... */
};
extern "C" {
Node *newIntLit(int val) {
return (Node*)new IntLit(val);
}
/* ... */
}
Actions createActions() {
Actions a;
a.newIntLit = &newIntLit;
/* ... */
return a;
}
现在我将它们放在extern "C" 中的唯一原因是因为我希望它们具有 C 调用约定。但最理想的是,我希望他们的名字仍然被破坏。它们永远不会从 C 代码中按名称调用,因此名称修改不是问题。将它们弄乱可以避免名称冲突,因为某些操作被称为 error,并且 C++ 回调函数具有如下丑陋的名称,以避免与其他模块发生名称冲突。
extern "C" {
void uglyNameError(char const *str) {
/* ... */
}
/* ... */
}
a.error = &uglyNameError;
我想知道是否可以仅通过给函数类型 C 链接来实现
extern "C" void fty(char const *str);
namespace {
fty error; /* Declared! But i can i define it with that type!? */
}
有什么想法吗?我正在寻找标准 C++ 解决方案。
【问题讨论】:
-
你不能将 Bison 输出编译为 C++ 代码,从而完全避免这个问题吗?
-
@Konrad 我的同事说,bison C++ 模式不好用,所以我们用纯 C 做这部分,并把它抽象出来,所以和扫描仪一起组成了一个纯 C 库.
标签: c++ calling-convention linkage