【发布时间】:2015-01-22 10:01:20
【问题描述】:
对于具有私有或受保护析构函数的类型,std::is_constructible 的预期结果是什么?
例如,即使只有朋友可以释放它,我仍然可以在堆上构造这样的对象:
#include <type_traits>
class Foo
{
friend void freeFoo(Foo*);
public:
Foo()
{}
private:
// Destructor is private!
~Foo()
{}
};
void freeFoo(Foo* f)
{
delete f; // deleting a foo is fine here because of friendship
}
int main()
{
Foo* f = new Foo();
// delete f; // won't compile: ~Foo is private
freeFoo(f); // fine because of friendship
if(!std::is_constructible<Foo>::value)
{
std::cout << "is_constructible failed" << std::endl;
}
}
在 gcc 和 Visual C++ (gcc demo on coliru) 上对 is_constructible 的最终检查将失败。
这是标准要求的行为吗?如果是这样,有没有办法检查该类型是否具有特定的构造函数,而不考虑析构函数上的访问说明符?
【问题讨论】:
-
@Caramiriel 不幸的是,它并没有直接解决问题。
is_destructible显式依赖析构函数的可访问性,这里不是这样。
标签: c++ c++11 language-lawyer typetraits