[英]How can i use template specialization within a non-templated class? c++
I have a class that is not a templated class, I need to add a function to this class that is a template function. 我有一个不是模板类的类,我需要在这个类中添加一个函数模板函数。 The problem is that I call a function in the class that requires a string as the parameter, so I need to make a specialized version of this template so I can call this function only if the parameter is a const char*
or make a conditional check inside the function to only make a call to the function if the parameter is a const char*
but that does not seem to work either. 问题是我在类中调用了一个需要字符串作为参数的函数,所以我需要创建一个这个模板的专用版本,所以只有当参数是一个const char*
或进行条件检查时我才能调用这个函数函数内部只调用函数,如果参数是一个const char*
但似乎也不起作用。 Any help would be appreciated! 任何帮助,将不胜感激!
template<class myType> __declspec(nothrow)
std::string GetStrVal(int row, int col, myType default) {
try {
CheckColumnType(col, String);
}
catch(DatatableException e){
return default;
}
return this->m_rows[row][col];
}
template<class myType>
std::string GetStrVal(int row, const char* col, myType default) {
unsigned int columnIndex = this->GetColumnIndex(col);
return GetStrVal(row,columnIndex, default);
}
GetColumnIndex()
only takes a const char*
. GetColumnIndex()
只接受一个const char*
。
You don't need to specialize anything; 你不需要专门化任何东西; you can just provide an overload or two: 你可以提供一两个重载:
template<class myType> __declspec(nothrow)
std::string GetStrVal(int row, int col, myType default); // template
__declspec(nothrow)
std::string GetStrVal(int row, int col, std::string default); // overload
__declspec(nothrow)
std::string GetStrVal(int row, int col, const char *default); // overload
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.