确定C++类是否具有私有析构函数

Myr*_*ria 6 c++ destructor private c++11

假设我有以下代码:

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");
}
Run Code Online (Sandbox Code Playgroud)

是否有可能实现IsDestructorPrivate上面的模板来确定一个类的析构函数是否private还是protected

在我正在使用的情况下,我需要使用它的唯一时间IsDestructorPrivate是在有权访问这种私有析构函数的地方(如果存在的话).它不一定存在.IsDestructorPrivate允许是宏而不是模板(或者是解析为模板的宏).C++ 11很好.

101*_*010 10

您可以使用std::is_destructible类型特征,如下例所示:

#include <iostream>
#include <type_traits>

class Foo {
  ~Foo() {}
};

int main() {
  std::cout << std::boolalpha << std::is_destructible<Foo>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

LIVE DEMO

std::is_destructible<T>::value将等于false如果的析构函数Tdeletedprivatetrue其它.