We are creating a domain specific language that generates C++ code that must compile under gcc and also IBM xlc (version 10.1 for AIX) compilers.
One specific code snippet generated C++ code that works perfectly well under gcc, but no so much under xlc. I have modified the code to the minimum case that still triggers the compile error:
//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;
}
Compiling this causes the following error to be raised: "bug183.cpp", line 21.66: 1540-0114 (S) A parameter name must not be the same as another parameter of this function.
but if I remove the enclosing parenthesis on the constructor call like this:
someClass nuni = someClass(otherClass(weco), otherClass(weco));
The error goes away. Also, if I change weco
for another extern variable created just as weco
the error goes away even if I enclose the constructor in parenthesis, so it is safe to say that both conditions need to be present for this error to show up.
Some of you may ask why don't we just remove the parenthesis then, but doing so may hurt parts of the code that are working correctly, so I'm inclined towards understanding if this behavior is expected from a C++ compiler or not or if at least there is a known workaround for it.