[英]'Adding Member Function to only Specialized Template of Class
I have a template class and I want to add a special function to a particular specialization of the template.我有一个模板类,我想为模板的特定专业添加一个特殊功能。 This function only makes sense for the std::string template.
这个函数只对 std::string 模板有意义。
I showed how I am handling this currently...the problem is that the deleted function is still visible to all template types, and not just the Match template that I want to use it with.我展示了我目前如何处理这个问题......问题是被删除的函数仍然对所有模板类型可见,而不仅仅是我想使用它的匹配模板。
Is there a more elegant way to handle this dilemma?有没有更优雅的方法来处理这个困境?
template<typename T>
class Match {
/*
*Class Definition
*
*/
void string_function() = delete;
};
template<>
void Match<Std::string>::string_function(){
//do something that only makes sense with strings
}
You can factor the common code in a base class template and make derived specialization with the tailored features.您可以在基类模板中分解公共代码,并使用定制功能进行派生专业化。
template <typename T>
class BaseMatch { /* Common features across all specializations... */ };
template <typename T>
class Match : BaseMatch<T> { /* Generic case, no custom behavior to enforce */ };
template <>
class Match<std::string> : BaseMatch<std::string> {
void string_function() { /* ... */ }
};
void string_function() = delete;
It will deallocate function from class scope, again you tring to define function.它将从类范围中释放函数,再次尝试定义函数。 It's like if you deleted a gmail account and try to login saying not able to login, that is not possible right, may I know the reason why you want to delete than can come up with some solution.
这就像如果您删除了一个 Gmail 帐户并尝试登录说无法登录,那是不可能的,我可以知道您要删除的原因,然后再想出一些解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.