Reputation: 860
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.
Upvotes: 0
Views: 161
Reputation: 39101
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)));
}
Upvotes: 1
Reputation: 4752
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;
}
Upvotes: 0