2010-08-09 52 views
5

我想用g ++ 4.4編譯並鏈接使用STL的簡單程序。 我正在嘗試使用-fno-implicit-templates,因此所有模板都必須顯式實例化。std :: map和-fno-implicit-templates

我不明白爲什麼代碼工作:

#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; 
} 

我希望,這個程序需要行:template class std::map<char,char>;,但該行不會使該程序的鏈接。需要std::_Rb_tree line。爲什麼?

在此先感謝,任何提示將不勝感激。

回答

7

地圖在其實現中使用紅黑樹,因此您必須顯式實例化map實例化所需的樹的類型。這看起來不像一個特別有用的編譯器標誌,恕我直言。

+3

我同意尼爾:這個標誌的問題是,你需要明確地實例化類的每個組件,這暴露你實現的每一個細節... URK。你很幸運,開始使用STL非常簡單,用一個強大的庫文件來嘗試它,在你編譯成功之前,你將會哭泣你所有的眼淚。 – 2010-08-09 13:24:38

+4

+1一個更誠實的國旗名稱將會是 - 你已經選擇了痛苦的方式 – 2010-08-09 16:18:56

+0

@chrispy:如果編譯器在Christopher Lee的聲音中讀出它會更好。 – Peter 2010-08-11 02:44:43