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)
std::is_destructible<T>::value
将等于false
如果的析构函数T
是deleted
或private
和true
其它.
归档时间: |
|
查看次数: |
548 次 |
最近记录: |