0

,當我試圖按照以下步驟創建一個模板類:類模板+函數模板

template <typename TList> 
class Variant 
{ 
public : 
    std::string toString(); // var.toString() 

    template<typename T> 
    std::string toString(); // var.toStrint<int>(); 

    protected: 
    template <typename T> 
    std::string toString(T v); // internal Specialization 

    template <> 
    std::string toString(int v); // internal Specialization 

    template <typename T> 
    const T & get() 
    { 
     std::size_t type_index = TypeListNamespace::IndexOf<TList, T>::value ; 

     if (type_index == max_num_of_t || type_index != _type_index) 
      throw std::bad_cast() ; 

     void * ptr = (void*) &_variant_holder; 
     T * vptr = reinterpret_cast<T *>(ptr); 
     return *vptr; 
    } 
}; 

// CPP FILE: 

template <typename TList> 
std::string Variant<TList>::toString() 
{ 
    // var.toString() 
} 

template<typename TList> 
template<typename T> 
std::string Variant<TList>::toString() 
{    
    return toString(get<T>()); 
} 

template<typename TList> 
template<typename T> 
std::string Variant<TList>::toString (T v) 
{     
    // no default toString method. 
    return ""; 
} 

template<typename TList> 
template<>  
std::string Variant<TList>::toString (int v) 
{     
    // Specialized toString for int values: 
    return Core::Utility::formatString("%i", v); 
} 

.. other specializations .. 

我得到了以下錯誤:

error C2244: 'Core::Variant<TList>::toString': unable to match function definition to an existing declaration 
2>   Definition 
2>   'std::string Core::Variant<TList>::toString(int)' 
2>   Available Deklarations 
2>   'std::string Core::Variant<TList>::toString(T)' 
2>   'std::string Core::Variant<TList>::toString(void)' 
2>   'std::string Core::Variant<TList>::toString(void)' 

當我有類定義這兩個特所有編譯馬上。所以我想我對模板語法做了錯誤。但是很難找到具有專業化的類和功能模板組合的例子。所以我最終在這裏希望有一個對我有好的暗示的人。

回答

0

看來,你不必把「模板<>」放在你的專業之上。

如果刪除它們,一切編譯罰款(找不要嘗試AT HOME)

template <typename TList> 
class Variant 
{ 
public : 
    std::string toString(); // var.toString() 

    template<typename T> 
    std::string toString(); // var.toStrint<int>(); 

    protected: 
    template <typename T> 
    std::string toString(T v); // internal Specialization 

    // DON'T TRY THIS AT HOME: template <> 
    std::string toString(int v); // internal Specialization 

    template <typename T> 
    const T & get() 
    { 
     std::size_t type_index = TypeListNamespace::IndexOf<TList, T>::value ; 

     if (type_index == max_num_of_t || type_index != _type_index) 
      throw std::bad_cast() ; 

     void * ptr = (void*) &_variant_holder; 
     T * vptr = reinterpret_cast<T *>(ptr); 
     return *vptr; 
    } 
}; 

// CPP FILE: 

template <typename TList> 
std::string Variant<TList>::toString() 
{ 
    // var.toString() 
} 

template<typename TList> 
template<typename T> 
std::string Variant<TList>::toString() 
{    
    return toString(get<T>()); 
} 

template<typename TList> 
template<typename T> 
std::string Variant<TList>::toString (T v) 
{     
    // no default toString method. 
    return ""; 
} 

template<typename TList> 
// DON'T TRY THIS AT HOME: template<>  
std::string Variant<TList>::toString (int v) 
{     
    // Specialized toString for int values: 
    return Core::Utility::formatString("%i", v); 
} 

.. other specializations .. 
0

正如你所說,你不需要模板<>,因爲你沒有專業功能。

你所做的是功能重載!