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

C++ 模板类和成员函数的部分模板专门化,c++,templates,C++,Templates,我已经有一个小时没有研究部分模板专业化的信息了。 不幸的是,这并不成功。。 我仍然发现了很多信息,但并没有解决我的问题。 所以我希望有人能帮助我 考虑以下最小代码: SQLObject.hpp template<typename T> class SQLObject { public: template<typename U> static std::list<T*> filter(const std::string

我已经有一个小时没有研究部分模板专业化的信息了。 不幸的是,这并不成功。。 我仍然发现了很多信息,但并没有解决我的问题。 所以我希望有人能帮助我


考虑以下最小代码:

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
{
公众:
模板
静态标准::列表过滤器(常量标准::字符串和列,常量标准::字符串和操作,常量U和值);
静态标准::列表过滤器(常量过滤器和过滤器);
}
#包括“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
}
#包括“Filter.hpp”
/*这个代码不起作用,但为什么*/
模板
模板
std::list SQLObject::filter(常量std::string&column,常量std::string&ope,常量std::string&value)
{
//不需要带std::string的字符串
返回筛选器(筛选器(列、操作、值));
}
模板
模板
std::list SQLObject::filter(常量std::string&column,常量std::string&ope,常量U&value)
{
//用于将字符串与所有其他类型一起使用
返回筛选器(筛选器(column、ope、std::to_字符串(值));
}
模板
std::list SQLObject::filter(常量过滤器和过滤器)
{
//一些东西
}

我的问题如下: 我无法使用std::string专门化筛选器

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

类似于(或者,正如上面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;
}
模板
类SQLObject
{
公众:
模板
静态标准::列表过滤器(常量标准::字符串和列,常量标准::字符串和操作,常量U和值);
};
/*这个代码不起作用,但为什么*/
模板
模板
std::list SQLObject::filter(常量std::string&column,常量std::string&ope,常量std::string&value)
{
//不需要带std::string的字符串
返回列表();
}
模板
模板
std::list SQLObject::filter(常量std::string&column,常量std::string&ope,常量U&value)
{
//用于将字符串与所有其他类型一起使用
返回筛选器(筛选器(column、ope、std::to_字符串(值));
}
int main(){
返回0;
}

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

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

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

//[...]
#包括
#包括
结构过滤器
{
//你这个构造器。。。
模板过滤器(T...{}
};
模板
类SQLObject
{
公众:
模板
静态标准::列表过滤器(常量标准::字符串和列,
常量std::字符串和操作、常量U和值);
//这里是过载
静态标准::列表过滤器(常量标准::字符串和列,
常量标准::字符串和操作,
常量std::字符串和值);
静态标准::列表过滤器(常量过滤器和过滤器);
};
//工作
模板
std::list SQLObject::filter(const std::string和column,
常量标准::字符串和操作,
常量标准::字符串和值)
{
//不需要带std::string的字符串
返回筛选器(筛选器(列、操作、值));
}
//[...]

但在这种特殊情况下,甚至还有一个更简单的解决方案:

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))); 
}
std::string const&to_string(std::string const&p){return p;}
//类别定义等。
模板
模板
std::list SQLObject::filter(const std::string和column,
常量标准::字符串和操作,常量U和值)
{
//用于将字符串与所有其他类型一起使用
使用std::to_字符串;
返回筛选器(筛选器(列、操作、到字符串(值));
}

你写了什么?你犯了什么错误?对你有帮助吗?