[英]Partial specialization of template class copy constructor
I'm trying to partially specialize the copy constructor of a very basic templated array class. 我试图部分专门化一个非常基本的模板化数组类的复制构造函数。
template<typename D, bool destruct = false> class SimpleArray
{
SimpleArray(const SimpleArray& other)
{
//stuff
}
//various other things
}
template<typename D> SimpleArray<D, true>::SimpleArray(const SimpleArray& other)
{
//different stuff
}
But I get this error: 但我得到这个错误:
'SimpleArray<D,destruct>::{ctor}' : unable to match function definition to an existing declaration
However, I've definitely declared the function... I tried changing the parameter in the partial specialization to const SimpleArray<D, true>& other
to no avail. 但是,我肯定已经声明了这个函数......我尝试将部分特化中的参数更改为
const SimpleArray<D, true>& other
无济于事。 VC++ 11 isn't highlighting the partially specialized function name itself, so I'm guessing the issue is an incorrect name somehow. VC ++ 11没有突出显示部分专用的函数名称本身,所以我猜测问题是某个错误的名称。
You cannot specialize a single member function of a certain partial specialization of a class template: if you want to go the specialization way, you have to specialize the whole class. 您不能专门化类模板的某个部分特化的单个成员函数:如果您想要使用特化方法,则必须专门化整个类。
However, you could decide to go the overload way instead, and differentiate between the case when destruct
is true
and when it is false
through tag dispatching . 但是,您可以决定采用过载方式,并在
destruct
为true
时通过标签分派将其区分为false
。
For instance, you could define two private member functions called copy_construct
, as shown below: 例如,您可以定义两个名为
copy_construct
私有成员函数,如下所示:
template<typename D, bool destruct = false>
class SimpleArray
{
// ...
private:
void copy_construct(const SimpleArray& other, std::true_type)
{
// ...
}
void copy_construct(const SimpleArray& other, std::false_type)
{
// ...
}
};
And then you could let the copy constructor of your class call the appropriate overload based on the value of the destruct
parameter: 然后你可以让你的类的复制构造函数根据
destruct
参数的值调用适当的重载:
template<typename D, bool destruct = false>
class SimpleArray
{
public:
// ...
SimpleArray(const SimpleArray& other)
{
copy_construct(other, std::integral_constant<bool, destruct>());
}
// ...
};
This will invoke the appropriate version of copy_construct()
, which will do what ought to be done based on the value of the destruct
parameter. 这将调用相应版本的
copy_construct()
,它将根据destruct
参数的值执行应该执行的操作。
The compiler message is not helpful (and is no better from G++ or Clang) but it's trying to tell you that you can't define a member function of a partial specialization if you haven't already declared the partial specialization. 编译器消息没有帮助(并且从G ++或Clang开始并不是更好),但它试图告诉您,如果尚未声明部分特化,则无法定义部分特化的成员函数。
ie you cannot specialize a single member function of a class template, you have to declare a specialization of the whole class template. 即,您不能专门化类模板的单个成员函数,您必须声明整个类模板的特化。 The reason is that the constructor is not a template, it's a normal (non-template) member function of a template.
原因是构造函数不是模板,它是模板的普通(非模板)成员函数。 To define a partial specialization you have to specialize the whole class template, you can't specialize a non-template.
要定义部分特化,您必须专门化整个类模板,不能专门化非模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.