简体   繁体   中英

partial template specialisation on template class and member function

It's been an hour since I am doing some research for informations about partial template specialisation. Unfortunately this is not successful .. I still found a lot of information, but not to solve my problem. So I hope that someone can help me.


Consider the following minimal code:

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
}

My problem is the following: I am not able to specialize filter with std :: string.

So I tried a simple overload, but without success. So I turn to you, hoping that you could help me.

Short answer: You can't explicitly specialize a member template of an class template that is not explicitly specialized.

I guess using an overload as you suggested might be the simplest solution:

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

//[...]

But in this particular case, there's even a simpler solution than that:

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

Similar to this question (or this question , as pointed out by DyP above). Here is a compiling specialization with the class specialized to 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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