C++ Compiler Inconsistencies

Return to my homepage.


I have come across some code that compiles with Visual C++ 6.0 (98) but not with g++ (various versions on linux, cygwin and solaris).


The first issue has to do with passing the return value of one function into another by reference. This code does not compile with g++ 3.3.1 or 2.9x. Download the file ObjectRef.cpp

// Test case demonstrating non portable code from
// Pablo/m3d/src/M3DObject.cpp

class MyObj {
public:

  MyObj() {}
  virtual ~MyObj() {}

};


void g(MyObj& x) {
}

void object_ref_test() {
  // assigning the result to a variable before passing it
  // by reference works on both platforms
  MyObj x = MyObj();
  g(x);
  // passing the result by reference without storing it
  // is valid under MSVC 6.0 but not g++
  g(MyObj());
}


The second issue has to do with how a friend class is specified. This code does compile on a linux box in cs with g++ 2.96. It also does compile on a solaris host with g++ 2.95.3. It does not compile under cygwin on my personal machine (3.3.1) nor on my personal linux machine (also 3.3.1). Download the file Friend.cpp.

//test case demonstrating non-portable code from
//Pablo/register/include/M3DDeformationProblem.h

class A {
  // this compiles with both compilers
  friend class B;
};

class B {
//this compiles with MSVC and G++ 2.9.x but not with g++ 3.3.1
  friend A;
};


With G++ I try to compile with the following command:

g++ -c Friend.cpp ObjectRef.cpp

I get the following error results:
Friend.cpp:12: error: friend declaration requires class-key, i.e. `friend class 
   A'
ObjectRef.cpp: In function `void object_ref_test()':
ObjectRef.cpp:23: error: could not convert `MyObj()' to `MyObj&'
ObjectRef.cpp:13: error: in passing argument 1 of `void g(MyObj&)'


With MSVC I try to compile with the following command:

cl /GX /c Friend.cpp ObjectRef.cpp

I get the following successful results:
Friend.cpp
ObjectRef.cpp
Generating Code...


Last Updated: Feb. 24, 2004