如何在模板化类中专门化模板化方法? 我发现C++模板很难把我的脑袋包起来。现在,我尝试专门化模板类的模板成员函数: template <bool COW> class BaseReferencedObject { public: BaseReferencedObject() : refCount(0) {} BaseReferencedObject(const BaseReferencedObject<COW>&) : refCount(0) {} BaseReferencedObject<COW>& operator=(const BaseReferencedObject<COW>&) {return *this;} template <bool COW2> void increaseRefCount() const; void decreaseRefCount() const {refCount--;} int getRefCount() const {return refCount;} private: mutable int refCount; }; class BaseReferencedObject<false>; class BaseReferencedObject<true >; template<> void BaseReferencedObject<false>::increaseRefCount<false>() const {refCount++;} template<> void BaseReferencedObject<true >::increaseRefCount<true >() const {refCount++;} typedef BaseReferencedObject<false> ReferencedObject; typedef BaseReferencedObject<true > SharedData ; 模板 类BaseReferenceObject{ 公众: BaseReferencedObject():refCount(0){} BaseReferencedObject(const BaseReferencedObject&):refCount(0){} BaseReferencedObject&运算符=(const BaseReferencedObject&){return*this;} 模板void increaseRefCount()常量; void decreaseRefCount()常量{refCount--;} int getRefCount()常量{return refCount;} 私人: 可变整数参考计数; }; 类BaseReferencedObject; 类BaseReferencedObject; 模板void BaseReferencedObject::increaseRefCount()常量{refCount++;} 模板void BaseReferencedObject::increaseRefCount()常量{refCount++;} typedef BaseReferencedObject ReferencedObject; typedef BaseReferencedObject共享数据;
然而,当我试图编译这段代码时,我得到了如何在模板化类中专门化模板化方法? 我发现C++模板很难把我的脑袋包起来。现在,我尝试专门化模板类的模板成员函数: template <bool COW> class BaseReferencedObject { public: BaseReferencedObject() : refCount(0) {} BaseReferencedObject(const BaseReferencedObject<COW>&) : refCount(0) {} BaseReferencedObject<COW>& operator=(const BaseReferencedObject<COW>&) {return *this;} template <bool COW2> void increaseRefCount() const; void decreaseRefCount() const {refCount--;} int getRefCount() const {return refCount;} private: mutable int refCount; }; class BaseReferencedObject<false>; class BaseReferencedObject<true >; template<> void BaseReferencedObject<false>::increaseRefCount<false>() const {refCount++;} template<> void BaseReferencedObject<true >::increaseRefCount<true >() const {refCount++;} typedef BaseReferencedObject<false> ReferencedObject; typedef BaseReferencedObject<true > SharedData ; 模板 类BaseReferenceObject{ 公众: BaseReferencedObject():refCount(0){} BaseReferencedObject(const BaseReferencedObject&):refCount(0){} BaseReferencedObject&运算符=(const BaseReferencedObject&){return*this;} 模板void increaseRefCount()常量; void decreaseRefCount()常量{refCount--;} int getRefCount()常量{return refCount;} 私人: 可变整数参考计数; }; 类BaseReferencedObject; 类BaseReferencedObject; 模板void BaseReferencedObject::increaseRefCount()常量{refCount++;} 模板void BaseReferencedObject::increaseRefCount()常量{refCount++;} typedef BaseReferencedObject ReferencedObject; typedef BaseReferencedObject共享数据;,c++,templates,template-specialization,C++,Templates,Template Specialization,然而,当我试图编译这段代码时,我得到了 SharedPtr.hh:31:62:错误:在“之前应该有初始值设定项您需要添加另一个模板来专门化成员函数 template<> template<> void BaseReferencedObject<false>::increaseRefCount<false>() const {refCount++;} template<> template<> void BaseReferen
SharedPtr.hh:31:62:错误:在“之前应该有初始值设定项您需要添加另一个模板来专门化成员函数
template<> template<> void BaseReferencedObject<false>::increaseRefCount<false>() const {refCount++;}
template<> template<> void BaseReferencedObject<true >::increaseRefCount<true >() const {refCount++;}
导致您在注释中提到的有点神秘的编译器错误。如果您作为编译器,那么这些行的错误会更清楚:
错误:模板专门化需要“模板”
以及错误
错误:嵌套名称说明符中命名的类型“BaseReferencedObject”不完整
对于成员函数专门化。原因是您没有定义类模板专门化,而只是向前声明它们。但是,这不是必需的,因为您可以专门化成员函数,而无需提供完整的类模板专门化
下面是一个完整的工作代码示例:
#include <iostream>
template <bool COW>
class BaseReferencedObject {
public:
BaseReferencedObject() : refCount(0) {}
BaseReferencedObject(const BaseReferencedObject<COW>&) : refCount(0) {}
BaseReferencedObject<COW>& operator=(const BaseReferencedObject<COW>&) {return *this;}
template <bool COW2> void increaseRefCount() const { std::cout << "DEFAULT\n"; refCount++; }
void decreaseRefCount() const {refCount--;}
int getRefCount() const {return refCount;}
private:
mutable int refCount;
};
template<> template<> void BaseReferencedObject<false>::increaseRefCount<false>() const { std::cout << "FALSE\n"; refCount++;}
template<> template<> void BaseReferencedObject<true >::increaseRefCount<true >() const { std::cout << "TRUE \n"; refCount++;}
typedef BaseReferencedObject<false> ReferencedObject;
typedef BaseReferencedObject<true > SharedData ;
int main()
{
BaseReferencedObject<false> b0;
BaseReferencedObject<true > b1;
b0.increaseRefCount<false>();
b0.increaseRefCount<true>();
b1.increaseRefCount<true >();
b1.increaseRefCount<false>();
}
#包括
模板
类BaseReferenceObject{
公众:
BaseReferencedObject():refCount(0){}
BaseReferencedObject(const BaseReferencedObject&):refCount(0){}
BaseReferencedObject&运算符=(const BaseReferencedObject&){return*this;}
模板void increaseRefCount()常量{std::cout专门化应该是:模板void BaseReferencedObject::increaseRefCount()…
@πάνταῥεῖ: 这不是只专门化类吗?我想专门化类-方法组合,因为类和方法都是模板化的。很抱歉,我忽略了它的模板化函数。这不起作用;然后我得到SharedPtr.hh:31:28:错误:模板参数列表太多
,模板void BaseReferencedObject::increaseRefCount()常量{refCount++;}
取而代之。@hellogoodbay那是因为你的代码有一些其他微妙的问题。我通过充分解释并提供一个解决所有问题的工作示例扩展了答案。现在我让它工作起来了。谢谢!我的两个错误似乎是我在方法Special中使用了模板
而不是模板
但是为什么最好为increaseRefCount
提供一个默认实现呢?我似乎不需要它,我希望编译器在调用任何组合false–true或true–false时生成一个错误,以防止我意外地执行ad将该组合添加到我的程序中,这样我就不需要默认实现。为什么你认为我应该有一个呢?@hellogoodbay如果你确定不需要它们,请务必删除默认impl,但最好是显式删除它。请参阅更新的答案。好的,我选择了后者。非常感谢!
class BaseReferencedObject<false>;
class BaseReferencedObject<true >;
#include <iostream>
template <bool COW>
class BaseReferencedObject {
public:
BaseReferencedObject() : refCount(0) {}
BaseReferencedObject(const BaseReferencedObject<COW>&) : refCount(0) {}
BaseReferencedObject<COW>& operator=(const BaseReferencedObject<COW>&) {return *this;}
template <bool COW2> void increaseRefCount() const { std::cout << "DEFAULT\n"; refCount++; }
void decreaseRefCount() const {refCount--;}
int getRefCount() const {return refCount;}
private:
mutable int refCount;
};
template<> template<> void BaseReferencedObject<false>::increaseRefCount<false>() const { std::cout << "FALSE\n"; refCount++;}
template<> template<> void BaseReferencedObject<true >::increaseRefCount<true >() const { std::cout << "TRUE \n"; refCount++;}
typedef BaseReferencedObject<false> ReferencedObject;
typedef BaseReferencedObject<true > SharedData ;
int main()
{
BaseReferencedObject<false> b0;
BaseReferencedObject<true > b1;
b0.increaseRefCount<false>();
b0.increaseRefCount<true>();
b1.increaseRefCount<true >();
b1.increaseRefCount<false>();
}
template<> template<> void BaseReferencedObject<false>::increaseRefCount<true>() const = delete;
template<> template<> void BaseReferencedObject<true >::increaseRefCount<false >() const = delete;