views:

102

answers:

1

I am trying to compile with g++ 4.4 and link a simple program that uses the STL. I am trying to do it using the -fno-implicit-templates so all templates must be instantiated explicitly.

I don't understand why this code works:

#include <map>
//template class std::map<char,char>;
template class std::_Rb_tree<char, std::pair <char const, char>,
               std::_Select1st<std::pair<char const, char> >, 
               std::less<char>, std::allocator<std::pair<char const, char> > >;

int main() {
   std::map <char,char> table;
return 0;
}

I would expect that this program needs the line: template class std::map<char,char>;, however that line does not make the program link. The std::_Rb_tree line is needed. Why?

Thanks in advance, any hint will be appreciated.

+7  A: 

Maps use red-black trees in their implementation, so you have to explicitly instantiate the type of tree required for the instantiation of map. This does not look like a particularly useful compiler flag, IMHO.

anon
I agree with Neil: the problem of this flag is that you need to instantiate explicitly every component of the class, which exposes you to every single detail of the implementation... URK. You're lucky to begin with the STL which is quite simple, try it with a heavy boost library and you'll cry all the tears of your body before you'll succeed compiling.
Matthieu M.
+1 A more honest flag name would be --you-have-elected-the-way-of-pain
chrispy
@chrispy: would be even better if the compiler read it out in Christopher Lee's voice.
Peter