我们正在创建一种域特定语言,该语言生成必须在 gcc 和 IBM xlc(AIX 版本 10.1)编译器下编译的 C++ 代码。
一个特定的代码片段生成了在 gcc 下运行良好的 C++ 代码,但在 xlc 下就不行了。我已将代码修改为仍会触发编译错误的最小情况:
//bug183.h
class emptyList
{};
extern emptyList weco;
class otherClass
{
public:
otherClass();
otherClass(const int & p);
otherClass(const emptyList & e);
int geta() {return a;}
private:
int a;
};
class someClass
{
public:
someClass();
someClass(const someClass & other);
someClass(const otherClass & p1, const otherClass & p2);
void exportState();
private:
otherClass oc1;
otherClass oc2;
};
//bug183.cpp
#include "bug183.h"
#include <iostream>
emptyList weco = emptyList();
otherClass::otherClass() {a = 0;}
otherClass::otherClass(const int & p) {a = p;}
otherClass::otherClass(const emptyList & e) {a = 1000;}
someClass::someClass() {oc1 = otherClass(); oc2 = otherClass();}
someClass::someClass(const someClass & other) {oc1 = other.oc1; oc2 = other.oc2;}
someClass::someClass(const otherClass & p1, const otherClass & p2) {oc1 = p1; oc2 = p2;}
void someClass::exportState() {std::cout << oc1.geta() << " " << oc2.geta() << std::endl;}
int main()
{
someClass dudi;
dudi.exportState();
//this line triggers the error in xlc
someClass nuni = (someClass(otherClass(weco), otherClass(weco)));
nuni.exportState();
return 0;
}
编译它会引发以下错误:“bug183.cpp”,第 21.66 行:1540-0114 (S) 参数名称不得与此函数的另一个参数相同。
但是如果我像这样删除构造函数调用上的括号:
someClass nuni = someClass(otherClass(weco), otherClass(weco));
错误消失。此外,如果我更改另一个在错误消失时weco
创建的 extern 变量,即使我将构造函数括在括号中,所以可以肯定地说,需要同时存在这两个条件才能显示此错误。weco
你们中的一些人可能会问为什么我们不直接删除括号,但这样做可能会损害部分正常工作的代码,所以我倾向于理解这种行为是否来自 C++ 编译器,或者如果至少有一个已知的解决方法。