I noticed that the template specialization in .cpp files is discarded if compiler optimization is enabled. I found this in a large application, and I welded the problem with a simple example.
First I define a new class in obj.h
#ifndef _OBJ_H_
#define _OBJ_H_
class Obj { };
#endif
Then I define a new template function in templates.h
#ifndef _TEMPLATES_H_
#define _TEMPLATES_H_
template<typename T>
int get()
{
return 0;
}
#endif
... and specialization for the Obj class in templates.cpp
#include "templates.h"
#include "obj.h"
template<>
int get<Obj>()
{
return 1;
}
Then I call the main function:
#include <stdio.h>
#include "templates.h"
#include "obj.h"
int main()
{
printf("Get: %d\n", get<Obj>());
return 0;
}
Compiling this example with different levels -Ogives different outputs.
$ g++ -o a main.cpp templates.cpp -O0
$ ./a
Get: 1
$ g++ -o a main.cpp templates.cpp -O2
$ ./a
Get: 0
g++ clang. g++ 4.7.2 clang 3.4.
, , , , -O0 _Z3getI3ObjEiv, , ( ). < > , , : ? , undefined, , clang, g++ , .