[英]Template specialization with a specific templated class
Given the following simple C++ class: 给定以下简单的C ++类:
using namespace std;
template<class T1>
class ValueWrapper {
private:
T1 value_;
public:
ValueWrapper() {}
ValueWrapper(const T1& value) {
value_ = value;
}
ValueWrapper(const ValueWrapper<T1> &wrapper) {
value_ = wrapper.value_;
}
ValueWrapper& Set(const T1& value) {
value_ = value;
return *this;
}
T1 Get() const {
return value_;
}
};
I was trying to create a simple shared_ptr
wrapper for that class (ultimately allowing the developer to use the class without the dereferencing operator if desired). 我试图为该类创建一个简单的
shared_ptr
包装器(如果需要,最终允许开发人员使用该类而无需使用反引用运算符)。 While I've seen a few examples of wrapping a shared_ptr
, I couldn't find any that also used a specialization for a templated class. 虽然我看到了一些包装
shared_ptr
示例,但找不到用于模板化类的还使用了专门化功能的示例。
Using the class above, I created a ValueShared
class which derives from shared_ptr
: 使用上面的类,我创建了一个来自
shared_ptr
的ValueShared
类:
template<class T1>
class ValueShared : public shared_ptr<T1> {
public:
ValueShared& operator =(const T1& rhs) {
// nothing to do in base
return *this;
}
};
Then, I created a custom make_shared_value
function: 然后,我创建了一个自定义的
make_shared_value
函数:
//
// TEMPLATE FUNCTION make_shared
template<class T1, class... Types> inline
ValueShared<T1> make_shared_value(Types&&... Arguments)
{ // make a shared_ptr
_Ref_count_obj<T1> *_Rx = new _Ref_count_obj<T1>(_STD forward<Types>(Arguments)...);
ValueShared<T1> _Ret;
_Ret._Resetp0(_Rx->_Getptr(), _Rx);
return (_Ret);
}
But, here's the problem code: 但是,这是问题代码:
template<class T1, class ValueWrapper<T1>>
class ValueShared<ValueWrapper<T1>> : public shared_ptr<ValueWrapper<T1>>{
public:
ValueShared& operator =(const ValueWrapper<T1>& rhs) {
auto self = this->get();
self.Set(rhs->Get());
return *this;
}
};
I wanted to provide a specialization of the equals operator
here that was specialized to the ValueWrapper class (so that it would Get/Set the value from the right hand side value). 我想在这里提供
equals operator
的专门化,它专门用于ValueWrapper类(以便它将从右侧值获取/设置值)。
I've tried a few things, but the current error is: 我已经尝试了一些方法,但是当前错误是:
error C2943: 'ValueWrapper<T1>' : template-class-id redefined
as a type argument of a template
Maybe this isn't the proper approach, or maybe it's not possible? 也许这不是正确的方法,还是不可能?
Following should remove your error: 以下应该删除您的错误:
template<class T1>
class ValueShared<ValueWrapper<T1>> : public shared_ptr<ValueWrapper<T1>> {
public:
ValueShared& operator =(const ValueWrapper<T1>& rhs)
{
auto self = this->get();
self->Set(rhs.Get());
return *this;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.