2009-12-12 125 views
7

我完整的代碼太長,但這裏是一個片段,將反映我的問題的實質:實例化後模板的專業化?

class BPCFGParser { 
    public: 

    ... 
    ... 

    class Edge { 
    ... 
    ... 
    }; 


    class ActiveEquivClass { 
    ... 
    ... 
    }; 

    class PassiveEquivClass { 
    ... 
    ... 
    }; 

    struct EqActiveEquivClass { 
    ... 
    ... 
    }; 

    struct EqPassiveEquivClass { 
    ... 
    ... 
    }; 



    unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges; 
    unordered_map<PassiveEquivClass, Edge *, hash<PassiveEquivClass>, EqPassiveEquivClass> discovered_passive_edges; 

}; 

namespace std { 


template <> 
class hash<BPCFGParser::ActiveEquivClass> 
{ 

    public: 
     size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const { 

     } 
}; 

template <> 
class hash<BPCFGParser::PassiveEquivClass> 
{ 

    public: 
     size_t operator()(const BPCFGParser::PassiveEquivClass & pec) const { 

     } 
}; 

} 

當我編譯這段代碼,我得到以下錯誤:

In file included from BPCFGParser.cpp:3, 
       from experiments.cpp:2: 
BPCFGParser.h:408: error: specialization of ‘std::hash<BPCFGParser::ActiveEquivClass>’  after instantiation 
BPCFGParser.h:408: error: redefinition of ‘class     std::hash<BPCFGParser::ActiveEquivClass>’ 
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of  ‘class std::hash<BPCFGParser::ActiveEquivClass>’ 
BPCFGParser.h:445: error: specialization of  ‘std::hash<BPCFGParser::PassiveEquivClass>’ after instantiation 
BPCFGParser.h:445: error: redefinition of ‘class std::hash<BPCFGParser::PassiveEquivClass>’ 
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of  ‘class std::hash<BPCFGParser::PassiveEquivClass>’ 

現在我必須爲這些類專門化std :: hash(因爲標準std :: hash定義不包括用戶定義的類型)。當我在BPCFGParser類的定義之前移動這些模板專業化時,我會嘗試各種各樣的錯誤,並且在某處(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html)我讀到:

每當您使用類作爲模板參數時,聲明該類必須完整,而不是簡單地向前聲明。

所以我卡住了。我不能專注BPCFGParser定義後的模板,我不能在BPCFGParser定義之前將它們專門化,我怎麼能得到這個工作?


您需要向專業化轉移到一個內部類BPCFGParser內。這樣做符合兩個要求。

非常感謝您對

hash類的命名空間std中定義的答案:)。它不允許我在非命名空間範圍內專門化hash的模板。甚至以下:

template <> 
    class std::hash<ActiveEquivClass> { 
... 

沒有工作。當我附上了專業化與namespace std {},但是,它給出了奇怪的錯誤:

In file included from BPCFGParser.cpp:3, 
       from experiments.cpp:2: 
BPCFGParser.h:225: error: expected unqualified-id before ‘namespace’ 
experiments.cpp:7: error: expected `}' at end of input 
BPCFGParser.h:222: error: expected unqualified-id at end of input 

velocityreviews給出一個答案,有人聲稱,命名空間不能類中定義。所以我仍然堅持。

回答

5

您需要將特化移入BPCFGParser內部的類中。這樣做符合這兩個要求

  1. 專業化是ActiveEquivClass
  2. 完整定義使用專業化

實例之前後:

class BPCFGParser { 

    class ActiveEquivClass { 
    ... 
    }; 

    template <> 
    class hash<ActiveEquivClass> { 
    public: 
     size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const { 
     } 
    }; 
    ... 
    unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges; 

}; 
+0

非常感謝您的答案,但它不起作用。請參閱下面的說明。 – 2009-12-13 01:58:59

1

嘗試將哈希<>模板BPCFGParser類聲明之前的專業化代碼。該錯誤表示散列基於/usr/include/c++/4.3/tr1_impl/functional_hash.h中定義的std :: hash進行擴展;因此,在實例化之前不使用專業化。理想情況下,擴展模板之前,您的專業化代碼應該可用於編譯器。

0

無論何時您使用一個類作爲模板參數,該類的聲明必須是完整的,而不是簡單的前向聲明。

這實際上並不正確。模板參數必須是完整類型的限制通常取決於模板內容;但只要模板不包含非完整類型的代碼,使用不完整類型實例化模板並非非法。

接近你的問題的唯一方法是在上課前定義專業化,但後級定義實際的成員函數operator()。也就是說,

template <> 
class std::hash<BPCFGParser::ActiveEquivClass> 
{ 
public: 
    size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const; 
}; 

// definition of BPCFGParser 

template<> std::size_t std::hash<BPCFGParser::ActiveEquivClass>::operator()(const BPCFGParser::ActiveEquivClass & aec) const { 
} 

這也意味着,沒有嵌套類,因爲你不能轉發聲明一個嵌套類。

0

我有完全相同的問題,終於想出了一個解決辦法用戶定義的hash函子的解決方案,見下圖:

class Outer 
{ 
    // TC++PL, 4e, 19.4.1 : A friend must be previously declared in an enclosing scope or 
    // defined in the non-class scope immediately enclosing the class that is declaring it to be a friend. 
    struct Hash_inner; 

    class Inner 
    { 
     int i; 
     friend struct Hash_inner; 
    }; 

    struct Hash_inner 
    { 
     size_t operator()(const Inner& in) const 
     { return std::hash<int>()(in.i); } 
    }; 

    std::unordered_map<Inner, int, Hash_inner> um; 
}; 

而且我還是老樣子不知道是否有一個std ::哈希專業化的方針。任何人都可以弄明白嗎?

0

我知道這個問題很老,但我剛剛遇到同樣的問題,我想我會報告我的發現。

錯誤消息:

In file included from BPCFGParser.cpp:3, 
      from experiments.cpp:2: 
BPCFGParser.h:408: error: specialization of ‘std::hash<BPCFGParser::ActiveEquivClass>’  after instantiation 
BPCFGParser.h:408: error: redefinition of ‘class     std::hash<BPCFGParser::ActiveEquivClass>’ 
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of  ‘class std::hash<BPCFGParser::ActiveEquivClass>’ 
BPCFGParser.h:445: error: specialization of  ‘std::hash<BPCFGParser::PassiveEquivClass>’ after instantiation 
BPCFGParser.h:445: error: redefinition of ‘class std::hash<BPCFGParser::PassiveEquivClass>’ 
/usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of  ‘class std::hash<BPCFGParser::PassiveEquivClass>’ 

不抱怨你的類可言,它說,你不能專注的std ::哈希,因爲無論是對的std ::哈希或一個通用模板現有的專業化已經被使用 - 也就是說,有人已經在它被定義的點和你試圖專門化它的點之間使用它。

有一些文字描述如此this document的「詳細」部分中:

專業化必須在第一次使用前宣佈,將使 隱式實例

在我的情況下,問題不在於我的專業代碼,問題在於它的立場。一旦std :: hash已經被使用了,你就無法進一步專注於它了。

我發現如果我在包括<unordered_map>之後立即將我的專業代碼移動到其中,它就可以正常工作。

的小狗從執行可以移動的聲明非常接近列入<unordered_map>專業化的聲明分開的建議,實施可以稍後無論它的方便(後BPCFGParser在完全定義你的案件)。