2013-05-29 47 views
0

自從我對部分模板專業化的信息進行一些研究以來,已經過去了一個小時。 不幸的是,這是不成功的.. 我仍然發現了很多信息,但沒有解決我的問題。 所以我希望有人能幫助我。模板類和成員函數的部分模板專業化


考慮以下最少的代碼:

SQLObject.hpp

template<typename T> 
class SQLObject 
{ 
    public: 
     template<typename U> 
     static std::list<T*> filter(const std::string& colum,const std::string& ope,const U& value); 
     static std::list<T*> filter(const Filter& filter); 
} 
#include "SQLObject.tpl" 

SQLObject.tpl

#include "Filter.hpp" 

/* This code do not work, but why ??? */ 
template<typename T> 
template<> 
std::list<T*> SQLObject<T>::filter<std::string>(const std::string& colum,const std::string& ope,const std::string& value) 
{ 
    // no to_string need whith std::string 
    return filter(Filter(colum,ope,value)); 
} 

template<typename T> 
template<typename U> 
std::list<T*> SQLObject<T>::filter(const std::string& colum,const std::string& ope,const U& value) 
{ 
    //use to_string with all others types 
    return filter(Filter(colum,ope,std::to_string(value))); 
} 

template<typename T> 
std::list<T*> SQLObject<T>::filter(const Filter& filter) 
{ 
    //some stuff 
} 

我的問題是這樣的: 我不是能夠spe使用std :: string進行cialize過濾。

所以我嘗試了一個簡單的過載,但沒有成功。 所以我轉向你,希望你能幫助我。

+1

什麼了你寫?你得到了什麼錯誤? –

+0

[** this **](http://stackoverflow.com/a/5513109/420683)能幫助您嗎? – dyp

回答

1

簡答:你不能明確地專門化一個沒有明確專門化的類模板的成員模板。

我猜使用過載如你所說可能是最簡單的解決方案:

#include <list> 
#include <string> 

struct Filter 
{ 
    // you constructor... 
    template < typename... T > Filter(T...){} 
}; 

template<typename T> 
class SQLObject 
{ 
    public: 
     template<typename U> 
     static std::list<T*> filter(const std::string& colum, 
            const std::string& ope,const U& value); 
     // v-here-v is the overload 
     static std::list<T*> filter(const std::string& colum, 
            const std::string& ope, 
            const std::string& value); 
     static std::list<T*> filter(const Filter& filter); 
}; 

// works 
template<typename T> 
std::list<T*> SQLObject<T>::filter(const std::string& colum, 
            const std::string& ope, 
            const std::string& value) 
    { 
    // no to_string need whith std::string 
    return filter(Filter(colum,ope,value)); 
} 

//[...] 

但是,在這種特殊情況下,甚至還有比這更簡單的解決方案:

std::string const& to_string(std::string const& p) { return p; } 

// class definition etc. 

template<typename T> 
template<typename U> 
std::list<T*> SQLObject<T>::filter(const std::string& colum, 
            const std::string& ope,const U& value) 
{ 
    //use to_string with all others types 
    using std::to_string; 
    return filter(Filter(colum,ope,to_string(value))); 
} 
0

類似於this question(或this question,如上述DyP所指出的)。這裏有專門編寫int的專業編譯。

template<typename T> 
class SQLObject 
{ 
public: 
    template<typename U> 
    static std::list<T*> filter(const std::string& colum,const std::string& ope,const U& value); 
}; 

/* This code do not work, but why ??? */ 
template<> 
template<> 
std::list<int *> SQLObject<int>::filter<typename std::string>(const std::string& colum,const std::string& ope,const std::string& value) 
{ 
    // no to_string need whith std::string 
    return list<int *>(); 
} 

template<typename T> 
template<typename U> 
std::list<T*> SQLObject<T>::filter(const std::string& colum,const std::string& ope,const U& value) 
{ 
    //use to_string with all others types 
    return filter(Filter(colum,ope,std::to_string(value))); 
} 

int main() { 

    return 0; 
}