gcc version 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9) gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu3) Different partial template specialization result with different optimization options? 1. right result without optimization $g++ -O0 -g3 -c test.cpp $g++ -O0 -g3 -c a.cpp $g++ -O0 -g3 -o test test.o a.o $./test DEFAULT PARTIAL 2. wrong result with optimization $g++ -O2 -g0 -c test.cpp $g++ -O2 -g0 -c a.cpp $g++ -O2 -g0 -o test test.o a.o $./test DEFAULT DEFAULT ????Bug??? 3.if write three part in one file ,we get the right result $g++ -O2 -g0 -o test2 test2 $./test2 DEFAULT PARTIAL $g++ -O0 -g3 -o test2 test2 $./test2 DEFAULT PARTIAL //************ //a.h //************ #ifndef A_H #define A_H #include using namespace std; template class A { public: void echo() {cout << "ĬÈÏ" << endl;} } ; #endif //************ //a.cpp //************ #include "a.h" template <> void A<0>::echo() { cout << "ÌØ»¯" << endl; } //************ //test.cpp //************ #include "a.h" int main( int argc, char *argv[] ) { A<1> DEFAULT; A<0> PARTICULAR; DEFAULT.echo(); PARTICULAR.echo(); return EXIT_SUCCESS; } //************ //test2.cpp //************ #include using namespace std; template class A { public: void echo() {cout << "ĬÈÏ" << endl;} } ; template <> void A<0>::echo() { cout << "ÌØ»¯" << endl; } int main( int argc, char *argv[] ) { A<1> DEFAULT; A<0> PARTICULAR; DEFAULT.echo(); PARTICULAR.echo(); return EXIT_SUCCESS; }
this is not a bug.