我有一个模板类,我想为模板的特殊化添加特殊功能。此功能仅对std :: string模板有意义。
我向我展示了当前的处理方式……问题是删除的功能对于所有模板类型仍然可见,而不仅仅是我想使用它的匹配模板。
是否有更优雅的方式来解决这一难题?
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
}
答案 0 :(得分:4)
您可以在基类模板中考虑通用代码,并使用量身定制的功能进行派生的专业化处理。
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() { /* ... */ }
};
答案 1 :(得分:0)
void string_function() = delete;
它将从类范围中释放函数,再次尝试定义函数。 就像您删除了一个Gmail帐户并尝试登录时说无法登录一样, 那是不可能的,我想知道您要删除的原因,然后提出一些解决方案。