C++:显式调用模板参数的 typedef 的析构函数
C++: Explicitly call destructor of template parameter's typedef
我有以下内容:
template <typename X> struct A {
typedef X _X;
};
template <typename Y> struct B { // Y is struct A
typename Y::_X x;
void call_destructor () {
x.~Y::_X(); // This doesn't work
x.Y::~_X(); // This as well
}
};
哪个不编译,说
qualified type does not match destructor name
在调用前使用关键字typename
也不起作用。然而,以下编译:
template <typename Y> struct B {
typename Y::_X x;
typedef typename Y::_X __X;
void call_destructor () {
x.~__X(); // This works
}
};
有人可以向我解释为什么吗,有什么办法可以不用 typedef
?
你应该使用不同的方式调用析构函数
x.Y::_X::~_X()
以下编译对我来说很好:
template <typename X> struct A {
typedef X _X;
};
template <typename Y> struct B { // Y is struct A
typename Y::_X x;
void call_destructor () {
x.Y::_X::~_X(); // This as well
}
};
int main(){
B<A<int> > b;
b.call_destructor();
}
x.~Y::_X(); // This doesn't work
是一个语法错误,我相信编译器将其解析为在~Y
中调用_X
第二种情况,当你调用包含::
的析构函数†时,最后两个类型名称必须表示相同的类型
s.A::~B();
其中 A
和 B
必须是同一类型。 A
和 B
都在前面的说明符指定的范围内查找,如果有
x._X::~_X(); // error, can't find _X in current scope
合乎逻辑的解决方法是
x.Y::_X::~_X(); // error, _X is dependent name
x.typename Y::_X::~_X(); // error, typename cannot be here
因为 Y::_X
是 dependent name,所以需要 typename
来消除它作为类型的歧义,但是析构函数的语法不允许 typename
在表达。最终结果是您必须使用类型别名
using X = typename Y::_X;
x.~X();
另一方面,编写并忘记析构函数调用的最简单方法就是
x.~decltype(x)();
但是 gcc 和 msvc 编译失败。
† 更准确地说,伪析构函数调用
使用此方法我能够为分配数组中的每个对象调用析构函数,其中包含模板函数内未指定的依赖类型。
即:
template <typename CompoundType>
void process_data()
{
const size_t MAX_SIZE = some_value;
CompoundType* obj_list = new CompoundType[MAX_SIZE];
// Receive data into obj_list and process it...
CompoundType* p_record = records;
for (int i = 0; i < MAX_SIZE; i++)
{
// Below is the magic to call the destructors!
p_record->std::remove_reference<decltype(*p_record)>::type::~type();
p_record++;
}
delete[] obj_list;
}
注意:上面的模板函数是通过以下特化调用的,每个内部 class 动态分配内存:
process_data<A::One>();
process_data<B::Two>();
process_data<C::Three>();
ie: 内部 class 的名称在模板函数中是未知的,简单地尝试调用 ~CompoundType 根本不起作用。
我有以下内容:
template <typename X> struct A {
typedef X _X;
};
template <typename Y> struct B { // Y is struct A
typename Y::_X x;
void call_destructor () {
x.~Y::_X(); // This doesn't work
x.Y::~_X(); // This as well
}
};
哪个不编译,说
qualified type does not match destructor name
在调用前使用关键字typename
也不起作用。然而,以下编译:
template <typename Y> struct B {
typename Y::_X x;
typedef typename Y::_X __X;
void call_destructor () {
x.~__X(); // This works
}
};
有人可以向我解释为什么吗,有什么办法可以不用 typedef
?
你应该使用不同的方式调用析构函数
x.Y::_X::~_X()
以下编译对我来说很好:
template <typename X> struct A {
typedef X _X;
};
template <typename Y> struct B { // Y is struct A
typename Y::_X x;
void call_destructor () {
x.Y::_X::~_X(); // This as well
}
};
int main(){
B<A<int> > b;
b.call_destructor();
}
x.~Y::_X(); // This doesn't work
是一个语法错误,我相信编译器将其解析为在~Y
_X
第二种情况,当你调用包含::
的析构函数†时,最后两个类型名称必须表示相同的类型
s.A::~B();
其中 A
和 B
必须是同一类型。 A
和 B
都在前面的说明符指定的范围内查找,如果有
x._X::~_X(); // error, can't find _X in current scope
合乎逻辑的解决方法是
x.Y::_X::~_X(); // error, _X is dependent name
x.typename Y::_X::~_X(); // error, typename cannot be here
因为 Y::_X
是 dependent name,所以需要 typename
来消除它作为类型的歧义,但是析构函数的语法不允许 typename
在表达。最终结果是您必须使用类型别名
using X = typename Y::_X;
x.~X();
另一方面,编写并忘记析构函数调用的最简单方法就是
x.~decltype(x)();
但是 gcc 和 msvc 编译失败。
† 更准确地说,伪析构函数调用
使用此方法我能够为分配数组中的每个对象调用析构函数,其中包含模板函数内未指定的依赖类型。
即:
template <typename CompoundType>
void process_data()
{
const size_t MAX_SIZE = some_value;
CompoundType* obj_list = new CompoundType[MAX_SIZE];
// Receive data into obj_list and process it...
CompoundType* p_record = records;
for (int i = 0; i < MAX_SIZE; i++)
{
// Below is the magic to call the destructors!
p_record->std::remove_reference<decltype(*p_record)>::type::~type();
p_record++;
}
delete[] obj_list;
}
注意:上面的模板函数是通过以下特化调用的,每个内部 class 动态分配内存:
process_data<A::One>();
process_data<B::Two>();
process_data<C::Three>();
ie: 内部 class 的名称在模板函数中是未知的,简单地尝试调用 ~CompoundType 根本不起作用。