2013-05-29 4 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 
} 

Моя проблема заключается в следующем: Я не способный cialize фильтр с std :: string.

Так что я пробовал простую перегрузку, но безуспешно. Итак, я обращаюсь к вам, надеясь, что вы можете мне помочь.

+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; 
} 
Смежные вопросы