模板类和成员函数的部分模板专门化

partial template specialisation on template class and member function

本文关键字:专门化 成员 函数      更新时间:2023-10-16

这是一个小时以来,我做了一些关于部分模板专门化的信息研究。不幸的是,这并不成功…我还是找到了很多信息,但并没有解决我的问题。所以我希望有人能帮助我。


考虑以下最小代码:

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
}

我的问题如下:我不能用std:: string专门化过滤器。

所以我尝试了一个简单的超载,但没有成功。所以我向你求助,希望你能帮助我。

简短的回答:不能显式特化未显式特化的类模板的成员模板。

我想使用你建议的重载可能是最简单的解决方案:

#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))); 
}

类似于这个问题(或者是上面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;
}