Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » Interesting GCC 'bug'

Credits go to Chris Katko and Peter Hull for helping out!
This thread is locked; no one can reply to it. rss feed Print
Interesting GCC 'bug'
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I banged my head against the wall for a while with this one.

Imagine you have a header like this :

#include "DeclaredBase.hpp"
#include "Forward.hpp"

class Derived : public DeclaredBase {
   Forward* f2;
};

And every time you include it the compiler chokes on the Forward* line, saying :

main.cpp:16:4: error: 'Forward' does not name a type

I couldn't figure out what was going on, so I thought, well there must be something wrong with the Forward class. This is all it is :

class Forward {
   int blah;
};

Okay well obviously there's nothing wrong with that. Let's check out the base class :

class Forward;

class DeclaredBase {
   Forward* pforward;
public :
   Forward* Forward() {return pforward;}
};

Well the compiler didn't complain about DeclaredBase at all, but if you look closely, the member function declared with the name Forward compiles just fine, but it hides the class with the name Forward.

Even if the Forward class is fully declared, the member function hides the class. Here's the full example :

#SelectExpand
1 2 3class Forward { 4 float fwd; 5}; 6 7 8 9class DeclaredBase { 10 Forward* pforward; 11public : 12 Forward* Forward() {return pforward;} 13}; 14 15 16 17class Derived : public DeclaredBase {
18 Forward* f2;
19}; 20 21 22 23int main(int argc , char** argv) { 24 25 Derived d; 26 (void)d; 27 28 return 0; 29}

And the compiler message stays the same :

main.cpp:18:4: error: 'Forward' does not name a type
    Forward* f2;
    ^~~~~~~

I think the compiler should know better than that and complain, but it gives you a false error message, because the class Forward was clearly defined.

EDIT
Hahaha MSVC is junk. They fail just as hard.

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	E0757	function "DeclaredBase::Forward" is not a type name	Project1	c:\ctwoplus\progcode\test\temp\main.cpp	18	
Error	C2143	syntax error: missing ';' before '*'	Project1	c:\ctwoplus\progcode\test\temp\main.cpp	18	
Error	C4430	missing type specifier - int assumed. Note: C++ does not support default-int	Project1	c:\ctwoplus\progcode\test\temp\main.cpp	18	
Error	C2238	unexpected token(s) preceding ';'	Project1	c:\ctwoplus\progcode\test\temp\main.cpp	18	

Anyone else have any interesting compile bugs they'd like to share? ;D

Peter Hull
Member #1,136
March 2001

FWIW clang has a more helpful message, including how to fix it:

c++     eduardo.cc   -o eduardo
eduardo.cc:11:4: error: must use 'class' tag to refer to type 'Forward' in this
      scope
   Forward* f2;
   ^
   class 
eduardo.cc:6:22: note: class 'Forward' is hidden by a non-type declaration of
      'Forward' here
   public: Forward * Forward() {
                     ^
1 error generated.
make: *** [eduardo] Error 1

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Chris Katko
Member #1,881
January 2002
avatar

Clang really is a higher tier of compiler than the rest. GCC optimizes at all passes which actually throws away metadata that then cannot be used for error messages at later passes. It used to be the "common" thing to do (hence MSVC as well). Clang/LLVM is a smarter design after decades of learned lessons and spilled blood.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Kitty Cat
Member #2,815
October 2002
avatar

Too bad CMake doesn't support Clang, that really sucks.

Clang works just fine with CMake, I use it often. If you're on Linux and Clang isn't your default compiler, you can do:

CC=clang CXX=clang++ cmake [other cmake options here]

with a fresh build directory. CMake and the generated makefile will remember the compiler, so you only need to set CC and CXX again when starting a fresh build directory.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Kitty Cat
Member #2,815
October 2002
avatar

Any you can use on your system, I'd think. I've used Unix Makefiles and Ninja.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter Hull
Member #1,136
March 2001

What I'm getting at, are gcc's compile options compatible with clang?

Should be; the clang compiler driver is described as a 'drop-in substitute for gcc`
(https://clang.llvm.org/get_started.html)

Also on Windows there is clang-cl which is the equivalent drop-in replacement for Microsoft's compiler (cl)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

w.o.w. It's like someone made an Easy button :

{"name":"iStock_29117266_XXLARGE.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/a\/2af48fbc3417383d60c833763b30c9e5.jpg","w":1200,"h":1120,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/a\/2af48fbc3417383d60c833763b30c9e5"}iStock_29117266_XXLARGE.jpg

Go to: