dimanche 25 janvier 2015

std::is_constructible on type with non-public destructor

What is the expected result for std::is_constructible on a type with a private or protected destructor?


For instance, I can still construct such an object on the heap even though only a friend can free it:



#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;
}
}


The final check for is_constructible will fail on both gcc and Visual C++ (gcc demo on coliru).


Is that the required behavior by the standard? If so, is there any way to check whether the type has a specific constructor, regardless of the access specifier on the destructor?


Aucun commentaire:

Enregistrer un commentaire