确定c++类是否具有私有析构函数
Determining whether a C++ class has a private destructor
假设我有以下代码:
class Example
{
#ifndef PRIVATE_DESTRUCTOR
public:
#endif
~Example() { }
public:
friend class Friend;
};
class Friend
{
public:
void Member();
};
void Friend::Member()
{
std::printf("Example's destructor is %s.n",
IsDestructorPrivate<Example>::value ? "private" : "public");
}
是否可以实现上面的IsDestructorPrivate
模板来确定类的析构函数是private
还是protected
?
在我使用的情况下,我需要使用这个IsDestructorPrivate
的唯一时间是在可以访问这样一个私有析构函数的地方,如果它存在的话。它不一定存在。允许IsDestructorPrivate是宏而不是模板(或者是解析为模板的宏)。
你可以像下面的例子那样使用std::is_destructible
类型特征:
#include <iostream>
#include <type_traits>
class Foo {
~Foo() {}
};
int main() {
std::cout << std::boolalpha << std::is_destructible<Foo>::value << std::endl;
}
现场演示
如果T
的析构函数为deleted
或private
和true
,则 std::is_destructible<T>::value
等于false
。
相关文章:
- 析构函数是否会自动调用 delete[] C++?
- 循环中本地对象的析构函数是否保证在下一次迭代之前被调用?
- 文件流析构函数是否可以在C++中引发异常?
- 构造函数是否unique_ptr初始化原始指针unique_ptr析构函数是否也删除关联的原始指针?
- 析构函数是否可以在 const 对象上调用非 const 函数
- cpp 中的析构函数是否自动调用?即使析构函数没有提及非动态变量,它们也会被删除吗?
- 析构函数是否可以不调用特定字段的析构函数
- 动态分配(堆上)的对象的析构函数是否会在返回函数中自动调用
- 当类中存在虚函数时,隐式生成的析构函数是否也是虚拟的
- C++析构函数是否始终或仅在有时调用数据成员析构函数
- 根据C++标准,显式调用构造函数和析构函数是否安全
- std:map 析构函数是否调用键析构函数以及值析构函数?
- QNetworkAccessManager 析构函数是否中止当前请求
- 基类析构函数是否阻止生成移动构造函数
- 琐碎的析构函数是否会导致混叠
- 全局变量构造函数/析构函数是否需要线程保护
- 多态类的隐式析构函数是否可以成为虚拟的
- 默认的虚拟析构函数是否阻止编译器生成的移动操作
- 析构函数是否自动为成员变量解除分配堆内存
- 对象的析构函数是否释放用于创建对象的指针所指向的内存?