模板类和成员函数的部分模板专门化
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;
}
相关文章:
- 如何基于模板化类的基类专门化成员函数
- 根据成员容器的尺寸,专门化成员功能
- 专门化一个成员函数,而不是整个类
- c++多参数模板化的类成员专门化
- C++ : 如何为模板类 A 专门化成员函数来处理类似数组的类 A<T>< >?
- 使用已推导的模板参数专门化模板成员函数
- C++专门化成员功能
- 根据类成员的存在/不存在专门化C++模板
- 如何根据类的模板参数专门化模板成员函数?
- 如何为具有位字段成员的类专门化“swap”
- 成员和成员函数的模糊部分模板专门化
- C++中模板类的成员函数的专门化
- 专门化模板成员函数来处理std::string和char[]参数
- 如何专门化此模板成员函数
- C++-专门化Template类的成员函数
- 专门化成员指针模板参数解析
- 如何从专门化成员函数访问模板参数
- 当类有多个模板参数时,专门化成员模板
- 单独声明和定义模板函数的专门化:成员函数和非成员函数的不同行为
- 无法在 VS .NET 2008 中使用 boost::enable_if 专门化成员函数模板