[LC++]Compiles with g++-3.2 but later versions flakey. Why?
Carlo Wood
carlo at alinoe.com
Wed Oct 26 03:50:02 UTC 2005
On Tue, Oct 25, 2005 at 04:45:23PM +0930, Dr Mark H Phillips wrote:
> Hi,
>
> I've attached some test code below. It compiles fine with
> g++ version 3.2.3, but fails for later versions. The strange
> thing is that different versions fail in different cases. Have
> I uncovered genuine compiler bugs, or is there something else
> going on?
>
> Here's the errors I'm getting:
> $ g++-3.3 testit.cc
> testit.cc: In function `int main()':
> testit.cc:22: error: `dummyx::dummyFunct' cannot be used as a function
> $ g++-3.4 testit.cc
> testit.cc: In function `int main()':
> testit.cc:23: error: `dummyx::dummyFunct' cannot be used as a function
> $ g++-4.0 testit.cc
> testit.cc: In function ‘int main()’:
> testit.cc:23: error: ‘dummyx::dummyFunct’ cannot be used as a function
> $
>
> Thanks,
>
> Mark.
>
#include <iostream>
namespace dummyx {
struct Dummy {
Dummy() {}
};
struct DummyFunct {
int operator()(Dummy d) const {return 5;}
static DummyFunct& full() {static DummyFunct f; return f;}
};
static DummyFunct& dummyFunct=DummyFunct::full();
}
int main() {
::dummyx::Dummy const d;
std::cout<<"(dummyx::dummyFunct)(d) = "<<(dummyx::dummyFunct)(d)<<std::endl; // succeeds with all
std::cout<<"dummyx::dummyFunct(d) = "<<dummyx::dummyFunct(d)<<std::endl; // fails with 3.3
std::cout<<"dummyFunct(d) = "<<dummyFunct(d)<<std::endl; // fails with 3.4 and 4.0
}
I had not much time last time, lemme add some remarks now.
Obviously, the correct way to interpret dummyx::dummyFunct(d) is to first
evaluate dummyx::dummyFunct and then call operator()(Dummy) on it. Thus,
it should do the same as (dummyx::dummyFunct)(d).
Because dummyFunct is only declared in namespace dummyx, it is not
declared in main(), hence, the last line should always fail.
The error is a bit weird though "cannot be called as function"
should be "is not declared". The reason that 3.3 also fails on
dummyx::dummyFunct(d) is because it's buggy and tries to evaluate
dummyFunct(d) before dummyx::dummyFunct (is my guess).
--
Carlo Wood <carlo at alinoe.com>
More information about the tuxCPProgramming
mailing list