假设我有以下代码:
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
的时间是在可以访问此类私有(private)析构函数(如果存在)的地方。它不一定存在。 IsDestructorPrivate可以是宏而不是模板(或是解析为模板的宏)。 C++ 11很好。 最佳答案
您可以像下面的示例一样使用std::is_destructible
类型特征:
#include <iostream>
#include <type_traits>
class Foo {
~Foo() {}
};
int main() {
std::cout << std::boolalpha << std::is_destructible<Foo>::value << std::endl;
}
如果
std::is_destructible<T>::value
的析构函数是false
或T
,而deleted
则为private
,则true
将等于ojit_code。关于c++ - 确定C++类是否具有私有(private)析构函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25317300/