繁体   English   中英

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

[英]partial template specialisation on template class and member function

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


考虑以下最少的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM