The specialization of a function is not called if compile with -03 option, the generic implementation is called insteed. This appear only if the declaration of the specialization is in another file. The specialization is called if compiled with -02 or lees optimization, or if the generic implementation is declared with the buildin noinline. Configuration of gcc -v: Using built-in specs. Target: i686-pc-linux-gnu Configured with: ../src/configure --prefix=/project/spg/tools/Open-Sources/gcc/4.1.1/Linux-x86/ --enable-__cxa_atexit --enable-shared --enable-long-long --enable-threads --enable-languages=c,c++ --with-gnu-as --with-gnu-ar --with-gnu-ld --with-gcc Thread model: posix gcc version 4.1.1 Three files: header #ifndef _TCLASS_H_ #define _TCLASS_H_ template<typename T> bool foo(T val) { return false; } #endif scpecialization: #include "tclass.h" template<> bool foo(int val) { return true; } main: #include "tclass.h" int main() { return(foo(1)); }
Created attachment 11829 [details] The main, just call the template function foo and return the return value
Created attachment 11830 [details] the specialization The specialization on int return true
Created attachment 11831 [details] The header with the generic implementation The generic template function return false
Created attachment 11832 [details] The makefile to test This makefile create two executable TC1 compiled with -02 which work well TC2 compiled with -O3 which don't
This bug was firstly discover under gcc3.2.3, then we test with 4.1.1 with the same results. The test case is very short.
You need to make the specialization visible at the point of instantiation.
Thanks, for you quick answer Richard. Where i don't understand is, it is working well if i declare the generic template function with: template<typename T> bool foo(T val) __attribute__(noinline) { return false; } Just in the case this workaround (perhaps evil) may interest some people. I return to read the C++ spec this night, i missed this point.