2010-06-14 131 views
4

我有了一個需要的專業,如在模板成員函數模板類:專門研究模板類的模板成員函數?

template <typename T> 
class X 
{ 
public: 
    template <typename U> 
    void Y() {} 

    template <> 
    void Y<int>() {} 
}; 

Altough VC可以正確處理此,apperantly這不是標準,GCC抱怨:explicit specialization in non-namespace scope 'class X<T>'

我嘗試過:

template <typename T> 
class X 
{ 
public: 
    template <typename U> 
    void Y() {} 
}; 

template <typename T> 
// Also tried `template<>` here 
void X<T>::Y<int>() {} 

但是這會導致VC和GCC抱怨。

什麼是正確的方法來做到這一點?

回答

8

非常普遍的問題。解決這個問題的方法之一是通過超載

template <typename T> 
struct type2type { typedef T type; }; 

template <typename T> 
class X 
{ 
public: 
    template <typename U> 
    void Y() { Y(type2type<U>()); } 

private: 
    template<typename U> 
    void Y(type2type<U>) { } 

    void Y(type2type<int>) { } 
}; 
+1

感謝。不幸的是,我的模板參數實際上是一個整數(如'模板'),不認爲它會有所作爲。有任何想法嗎? – uj2 2010-06-14 19:56:06

+1

只是用'int N'替換'typename T'和'U',而且你服務得很好:)這就像打字方式一樣工作。 – 2010-06-14 19:58:25

+0

@ JohannesSchaub-litb,如果'T'和一個非類型模板arg都被*使用了?如在類中,類是'template ',但函數是'template ',並且您希望專門爲該函數上的'<0>',同時保持類在'T'上打開。那可能嗎?謝謝。 – WhozCraig 2014-09-09 11:51:32

0

使用輔助類刪除類專業化

enum class Side {                                                                      
    BUY = 0,                                                                       
    SELL                                                                        
};                                                                          

using PriceType = int64_t;                                                                    

class BookHelper {                                                                      

public:                                                                        

BookHelper(                                                                       
    PriceType insideBuyPrice,                                                                   
    PriceType insideSellPrice)                                                                   
:                                                                          
    insideBuyPrice_{insideBuyPrice},                                                                 
    insideSellPrice_{insideSellPrice}                                                                 
{ }                                                                         

template <Side SideV>                                                                     
PriceType insidePrice() const;                                                                   

template <Side SideV>                                                                     
void insidePrice(PriceType price);                                                                  

private:                                                                        
    PriceType insideBuyPrice_;                                                                   
    PriceType insideSellPrice_;                                                                   

};                                                                          

template <>                                                                       
inline void BookHelper::insidePrice<Side::BUY>(PriceType price) {                                                          
    insideBuyPrice_ = price;                                                                   
}                                                                          

template <>                                                                       
inline void BookHelper::insidePrice<Side::SELL>(PriceType price) {                                                          
    insideSellPrice_ = price;                                                                   
}                                                                          

template <>                                                                       
inline PriceType BookHelper::insidePrice<Side::BUY>() const {                                                           
    return insideBuyPrice_;                                                                    
}                                                                          
                                                                             template <>                                                                       
inline PriceType BookHelper::insidePrice<Side::SELL>() const {                                                           
    return insideSellPrice_;                                                                   
}                                                                          

template <typename BookT>                                                                    
class Book {                                                                       

public:                                                                        
    Book();                                                                        

    template <Side SideV>                                                                    
    PriceType insidePrice() const;                                                                  

    template <Side SideV>                                                                    
    void insidePrice(PriceType price);                                                                 

private:                                                                        
    std::unique_ptr<BookHelper> helper_;                                                                

};                                                                          

template<typename BookT>                                                                    
Book<BookT>::Book() :                                                                     
    helper_{new BookHelper{std::numeric_limits<PriceType>::min(), std::numeric_limits<PriceType>::max()}}                                                
{}                                                                          

template <typename BookT>                                                                    
template <Side SideV>                                                                     
PriceType Book<BookT>::insidePrice() const {                                                               
    return helper_->insidePrice<SideV>();                                                                
}                                                                          

template <typename BookT>                                                                    
template <Side SideV>                                                                     
void Book<BookT>::insidePrice(PriceType price) {                                                              
    helper_->insidePrice<SideV>(price);                                                                 
}                                                                          

class TestBook { };                                                                     

int main() {                                                                       
    Book<TestBook> test;                                                                    
    test.insidePrice<Side::SELL>(1230046);                                                                
    test.insidePrice<Side::BUY>(1230045);                                                                

    std::cout << " inside SELL price : " << test.insidePrice<Side::SELL>() << std::endl;                                                    
    std::cout << " inside BUY price : " << test.insidePrice<Side::BUY>() << std::endl;                                                     
}