2011-03-27 32 views
2
//more code omitted that is not relevant, the IF template is not completely shown here 

template <bool condition, typename ThenType, typename ElseType> 
    struct IF { 
     typedef typename ChooseSelector<condition>::RETURN Selector; 
    }; 

    template <bool condition> 
     struct ChooseSelector { 
      typedef SelectThen RETURN; 
     }; 

    template <> 
     struct ChooseSelector<false> { 
      typedef SelectElse RETURN; 
     }; 
//SelectElse and SelectThen omitted 

我得到Expected nested-name-specifier before ‘ChooseSelector’。根據經常聯繫C++ typename description,如果我得到它正確的,這裏需要typename。如果我從IF模板中刪除typename,我仍然得到相同的錯誤,所以我有點困惑實際上導致錯誤的原因。我讀了很多答案,表明刪除typename可以解決問題,但在這種情況下情況並非如此。我錯過了什麼?與typename混淆,因此錯誤

錯誤來自Linux上的g ++,VS10也會拋出錯誤。

+1

把你的IF模板放在ChooseSelector模板後 – Erik 2011-03-27 10:47:55

+0

嗯,工作。不是那樣想的。如果你把它作爲答案發布,我會承認它。你能告訴我爲什麼這是相關的嗎? – DrColossos 2011-03-27 10:51:41

回答

2

將您的IF模板放在ChooseSelector模板之後。

編譯IF模板時,ChooseSelector需要作爲模板存在,您使用的是第一遍解析的ChooseSelector<condition>。需要typename來告訴編譯器RETURN,當已知專業化時,在實例化時對其進行全面評估,應將其視爲第一遍的類型。