gnu g++ compiler bug

Discovered a g++ compiler bug that can be reproduced on (gcc 4.1.2 default instalation on RH5, and gcc 4.3.5).

Given the following code:


template
class Wrapper
{
public: Wrapper( Wrapped *c );

Wrapped *c;
};

class Stream
{

public:
Stream(const Wrapper &cucucucu) {}

Stream &operator << (const std::string &s)
{
std::cout << s << std::endl;
return *this;
}
};

int main()
{
unsigned char *buffer;
//A a(cccc);

Stream str( Wrapper (buffer));

std::string s(”aoeuao”);
str << s;
}

the compiler throws the following misterous error:

t.cpp: In function 'int main()':
t.cpp:35: error: no match for 'operator<<' in 'str << s'

01
mobiphil
March 29th, 2010 6:13 am

The secret is that

Stream str( Wrapper (buffer));

is considered by the compiler as function definition and not variable declaration :)

02
George
May 3rd, 2010 4:53 am

Of course it does,

It’s not a bug the standard clearly states that when a definition can be read as a function or a variable declaration. The earlier SHALL take precedence, no exceptions to the rule.

g++ adheres to the standard

Leave Your Comment

Name*
Mail*
Website
Comment